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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 65

Thread: case

  1. #1
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question case

    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?


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    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)

  3. #3
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    ok,
    public String bottle(int bottle)
    ^
    illegal start of expression

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: case

    Thats not enough code for us to help you.

  5. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    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)

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: case

    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 }.

  7. #7
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    fine, here is more code

    public String bottle(int bottle)
    {
    switch (bottle)
    {
    case 1:
    return one;
    ...
    case 19:
    return nineteen;

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: case

    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.

  9. #9
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    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)

  10. #10
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    its also saying that i need simcolons in these places:

    public String bottle(int bottle)
    ^ ^

  11. #11
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    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)

  12. #12
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    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.

  13. #13
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: case

    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.

  14. #14
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    that is it

  15. #15
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: case

    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.

  16. #16
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    case 1:
    return one*
    lllllllllllll^
    ?? What is one*? Is it a variable? Like Kucerack said, if the return is void and you request any part
    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)

  17. #17
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    i want to create a string out of a variable.

  18. #18
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: case

    Quote Originally Posted by lucas29 View Post
    i want to create a string out of a variable.
    Which variable? The toString() method can be used on most non-primitive variables to convert them to a String object, so you might try that, BUT I'm not sure how your response relates to KucerakJM and Ada's points.

  19. #19
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    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.

  20. #20
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    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)

  21. #21
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: case

    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.
    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.

    If you can't be more clear, this thread will be closed.

  22. #22
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    i am changing an int variable into a string

  23. #23
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: case

    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.

  24. #24
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: case

    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)

  25. #25
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: case

    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?

Page 1 of 3 123 LastLast

Similar Threads

  1. case 3
    By ericgomez in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2013, 06:21 PM
  2. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:03 AM
  3. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  4. Please help me with my Case Study
    By dalexquisite in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2012, 11:58 PM
  5. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 11:54 PM