Please post your code correctly using code or highlight tags which are explained near the top of this link.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please post your code correctly using code or highlight tags which are explained near the top of this link.
The main method is the entry point for a program in Java and if it isn't there it will get mad at you. Create the main method and then use the writeoutput method inside of it.
here is the exact book name with all the info:
absolute java
by: Walter Savich
page: 210
this page has a code that they said works but it has no main, What do i do?!
The purpose of Chapter 4 is to introduce and explain the construction and usage of multiple classes in one program. At this point in the book, the student should discover that the main() method may be contained in another class, often called ClassNameDemo in this book.
main is required in every single Java program - it acts as the starting
point for the application. Thank you for finally posting the full code -
btw please use code tags when posting code.
You main method should be placed in the above class - or in a separate
file related to this project using the same package name.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
here is the new code and problem:
import java.util.scanner;
{
public static void main (String[] args)
{
int bottle;
int bottlezero;
int i;
int bottletens;
int bottleones;
String bottleword;
void writeoutput();
{
Scanner scannerObject = new Scanner(system.in);
System.out.println("how many bottles of beer?");
bottle = scannerObject.nextInt();
if (bottle <= 0);
{
bottle = 0;
}
if (bottle > 99);
{
bottle = 99;
}
bottletens = bottle/10;
bottlleones = bottle%10;
for (i = bottle; i > 0; i--)
{
System.out.println(i + "bottles of beer on the wall, " + i + " bottles of beer!");
System.out.println("you take one down pass it around, " + (i - 1) + "bottles of beer on the wall!");
}
System.out.println("the song is done");
}
}
public String bottlewordString(int bottleNumber)
{
if (bottle < 20)
{
switch (bottlezero)
{
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
...
...
case 19:
return "nineteen";
default:
System.out.println("please enter a different number");
System.exit(0);
return "error"; //to keep the compiler happy
}
}
else
{
switch (bottletens)
{
case 2:
return "twenty";
...
case 9:
return "ninety";
default:
System.out.println("please enter a different number");
System.exit(0);
return "error"; //to keep the compiler happy
}
switch (bottleones)
{
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
...
case 9:
return "nine";
default:
System.out.println("please enter a different number");
System.exit(0);
return "error" //to keep the compiler happy
}
}
}
}
--- Update ---
void writeOutput();
^
illegal start of expression
Your asking the compiler to treat the method as a variable.
Get rid of the semi-colon after the parentheses and it will work.
Also - please use code tags,
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
its doing the same thing
Sorry - misread the code.
You cannot invoke a method within a method. It's illegal.
Move the method outside of the main method so it has it's own
scope.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
is the void writeOutput() neccesary?
Yes, it's a method declaration. The compiler needs to know
what the method is called and what (if any) parameters are going
to be passed to it and their viable types.
Method main has a declaration too - look at the first line of the method.
String[] and args are parameters of the method main.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
then please tell me where i should put the part out of the main
Nobody here (myself included) is going to hold your hand through
every program you write. I and others have given you plenty of advice and
tips to help you. Do you not have a book? Read up about how methods are
declared, and how they keep track of scope issues.
I more or less told you the answer in a previous post - you need to move
the code for that said method out of the main method scope. What do you
think the braces do in Java?
Please do not take my tone for being upset - I am just being honest and
trying to help you to help yourself. Show some updated code with what
you have tried to do - to solve this problem
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
i have been trying to do this for a while now and i just wanted to know if that part was necessary because i never used it before, i have written codes before, just not using that so i wanted to see if i could do the same thing here aswell.
btw i am cool with u anyway.
When you create a method which is not the main method
you need to write the executing code away from the main method.
All methods are encased in braces - the braces define the scope of all
the code in that method.
This is said to be local scope - as it is local to that said method. When the
method terminates, (the closing brace is met or a return statement is
executed) all variables local to that method are destroyed (unless they
are static). The only way to share information between methods is to
pass the data through arguments. These arguments are then used as
the parameters for the called method.
Is that any clearer?
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
so, move it to right after the main? right before the switch statement?
--- Update ---
inbetween those right?
If that switch statement (and I am guessing this is true) is part of
the other method, all that code that has nothing at all to do with
method main needs to be placed outside of the method main's
scope.
What I suggest is properly closing off method main first (closing brace)
Then put a comment - then put all the code for the other function
including the parameter header you wrote for it - get rid of the semicolon.
Do that, then if you have more problems by all means ask.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
speak english please, i am not understanding what u said
That was English....
If you mean the way I said it, well that is the only way I can logically
express what I meant without actually coding the answer for you.
I spoke in a "programmable like" speech - giving tips as best as I
could. What part(s) did you not understand?
Wished Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
You know what a comment is right? A description of what the
method will do.
When I said "put all code for the other function" I meant all the code
that belongs to the writeOutput() method should be placed in the writeOut()
method within the braces of that method - separate from the main method
of course. The compiler is complaining because (in English terms) Java does
not allow a method to be created within the body of another method.
Btw - when I said "function" I meant method.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
thanks for helping me understand, i really appreciate it
btw - i will message again if i need anymore help
No problem - and good luck with the rest of the program
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
Yes, because when you declare a method to have a return
type of void you are telling the compiler you do not wish
to return anything.
Whatever the value of the data is you want to return, the method
return type should mirror that.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)