I'm trying to create a code with case commands but it says that public is an illegal start of statement, What do i do?
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.
I'm trying to create a code with case commands but it says that public is an illegal start of statement, What do i do?
What do you mean "illegal start?" public is a keyword that allows access to code within
it's scope. Can you show us some source code which demonstrates the problem?
All Java programs are in class format - so public is required in most cases.
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)
ok,
public String bottle(int bottle)
^
illegal start of expression
Thats not enough code for us to help you.
Is that a method parameter? Part of a class perhaps? As a standalone
statement yes that would be illegal if used as an expression.
public class Example { public int x; // is this what your trying to do? public Example() { // constructor x = 0; } public int getField() { // this is a method parameter return x; } }
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)
The code you posted as the problem is likely NOT the problem. I suspect the problem occurs BEFORE that code, usually caused by misplaced or missing { or }.
fine, here is more code
public String bottle(int bottle)
{
switch (bottle)
{
case 1:
return one;
...
case 19:
return nineteen;
Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.
Please post your code correctly per the above link, and pull back the 'tude, dude.
Does the code you posted (improperly) have a close brace, }, and reside in a class? If so, post the whole class, including the code above, or SIMILAR, runnable code that demonstrates the problem.
Your telling the compiler you expect the method to return a String data type.
but is that really what your code is returning?
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 also saying that i need simcolons in these places:
public String bottle(int bottle)
^ ^
Can you post the whole code? There is no way to correctly guess what is going
on without knowing what the whole class/methods look like. The semicolon error
can mean logical or compilation errors - again, we need to see more code than you are
showing.
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 fixed one problem but got another
here it is:
case 1:
return 1
lllllllllllll^
cannot return a value from method whose result type is void,
it says this all throughout the cases and at the default part.
--- Update ---
i fixed one problem but got another
here it is:
case 1:
return one*
lllllllllllll^
cannot return a value from method whose result type is void,
it says this all throughout the cases and at the default part.
I think posting the exact error message may shed some light on what the root cause is. So, if you haven't figured out this problem please post said message.
that is it
The closest error message I can get, using Eclipse, is "Void methods cannot return a value" but so long as you didn't change public String bottle(int bottle) to public void bottle(int bottle) I am probably doing something different from you. Seeing your complete bottle method code may help in some way. My guess is either your method return type has been changed to void or the variable(s) you are trying to return are void. (though the former makes more sense) I suggest you double check that the return type of the method and the variable type that you are trying to return are the same.
?? What is one*? Is it a variable? Like Kucerack said, if the return is void and you request any partcase 1:
return one*
lllllllllllll^
of that said method to return a value you will get this error message. Removing all the 'return' statements
will solve it - but I thought you originally wanted the method to return a String type.
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 want to create a string out of a variable.
i read in a variable the user inputs and the i have to change it into the word form of that variable, that is my assignment.
whats happening is it's saying i can't put a string in there and i need to.
So what data type is the original input? You can convert most types into
another format temporarily using Java built in methods or a concept known
as casting. If the data input is a String type, then most of the translation
is done for you.
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)
And how has this thread been working towards that objective? Please be more clear on what you need help with. I recommend you post your assignment word for word, copied and pasted if possible, and then post your code that attempts to accomplish the assignment with any questions you have. So far we've made no progress, because you won't post the code you've written with a question and/or an error that makes sense.i read in a variable the user inputs and the i have to change it into the word form of that variable, that is my assignment.
If you can't be more clear, this thread will be closed.
i am changing an int variable into a string
There's nothing we can do with, "i am changing an int variable into a string," except to wonder why you're here. Show the code you've written so far to accomplish that with the questions you have.
I do not like the feeling that we are just getting 'stringed' along (pardon the pun).
Is there a legitimate reason why you cannot post the code or the part you are
getting stuck on?
Greg, myself and Kucerak have kindly suggested helpful tips, but it's like grasping
at thin air without any psychical code to look at. Greg has mentioned the thread will
be closed if no sensible guideline to your problem is specified - so please, help us
so others can help you.
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)
sorry i couldnt get to it with my laptop at the time, her is the whole code:
import java.util.Scanner;
public class beersong
{
public int bottle;
public int i;
public String bottleword;
public void writeOutput()
{
Scanner scannerObject = new Scanner(System.in);
System.out.println("how many bottles of beer?");
bottle = scannerObject.nextInt();
if (bottle <= o);
{
bottle = 0;
}
for (i = bottle; i > 0; i--
{
System.out.println(i + "bottles of beer on the wall, " + i + "bottles of beer!");
System.out.print("you take one down, pass it around " + (i - 1) + " bottle of beer on the wall!");
}
System.out.println("the song is done");
}
public String bottleword(int bottle)
{
switch (bottle)
{
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
case 10:
return "ten";
case 11:
return "eleven";
case 12:
return "twelve";
case 13:
return "thirteen";
case 14:
return "fourteen";
case 15:
return "fifteen";
case 16:
return "sixteen";
case 17:
return "seventeen";
case 18:
return "eighteen";
case 19:
return "nineteen;
default:
System.out.println("please enter a different number and try again");
System.exit(0);
return "error"; //to keep the compiler happy
}
}
}
the new error i am getting is that i need: public static void main(String[] args)
yet my book says i don't need that in the code they wrote.
what do i do?