Question:
Can someone check and see what is wrong with my Java program?
Clubs
2013-07-31 18:54:55 UTC
I am doing the following problem:
Write a complete Java program in a class named Mantra that produces the following output. Remove its redundancy by adding a method.
There's one thing every coder must understand:
The System.out.println command.

There's one thing every coder must understand:
The System.out.println command.

Here is what I think the answer is:
public class Mantra {
public static void main (Strings args[]){
printstatement1();
printstatement2();
printstatement1();
printstatement2();
}

public static void statement1 {
System.out.println("There's one thing every coder must understand:");
System.out.println("The System.out.println command.");
}
}

However, the website I am doing this program on tells me that there is an error so can anyone tell me what I am doing wrong and what the correct program would be. Thank you for any help you can offer.
Four answers:
25chars
2013-07-31 19:17:47 UTC
A few things need correcting. There is not a class called "Strings", it is "String" so your main method signature must change to:



public static void main (String args[]) {



Next, your main calls methods that don't exist. You have a method named statement1 yet you call printstatement1 and printstatement2.



I would rename your new method to printStatement and include an empty parameter list () like this:



public static void printStatement() {



And then in your main, call it twice.



The whole class then would look like this:



public class Mantra {

public static void main (String args[]) {

printStatement();

printStatement();

}



public static void printStatement() {

System.out.println("There's one thing every coder must understand:");

System.out.println("The System.out.println command.");

System.out.println();

}

}
alex
2013-08-01 02:30:55 UTC
Here's a nice and simple solution. It basically prints out the statement you wanted over 3 times, you can make it 2 if you want (based on the for loop) and has one printStatement function which you pass in the statement you want printed out.



public class Mantra {



public static void main(String[] args) {

String statement = "There's one thing every coder must understand: \nThe System.out.println command.";

for(int i =1; i <= 3; i++){

printStatement(statement);

}

}



public static void printStatement(String statement){

System.out.println(statement);

}

}



I also made the statement into 1 string, you could have just as easily separated it into 2 strings and not used a for loop



String statement1 = "There's one thing every coder must understand:";

String statement2 = "The System.out.println command.";

printStatement(statement1);

printStatement(statement2);

printStatement(statement1);

printStatement(statement2);

printStatement(statement1);

printStatement(statement2);



The first solution is 4 lines long an gives you flexibility on how many times you want it printed and the second solution is 8 lines long and requires you to manually copy and paste the amount of times the statement can be printed
Puter_Notgeek
2013-08-01 02:18:02 UTC
You are calling a method

printstatement1();



But your method is the wrong name and no braces ()

public static void statement1



Should be

public static void printstatement1()



And did they want 2 print methods?

Then do this



public static void printstatement1() {

System.out.println("There's one thing every coder must understand:");

}



public static void printstatement2() {

System.out.println("The System.out.println command.");

}



Not sure what they want...but fixed the mistakes I saw.
Fabiio
2013-08-01 01:55:18 UTC
hajsn


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...