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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 65

Thread: case

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

    Default Re: case

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

  2. #27
    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 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.

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

    Default Re: case

    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?!


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

    Default Re: case

    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.

  5. #30
    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

    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)

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

    Default Re: case

    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

  7. #32
    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 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)

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

    Default Re: case

    its doing the same thing

  9. #34
    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

    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)

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

    Default Re: case

    is the void writeOutput() neccesary?

  11. #36
    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

    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)

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

    Default Re: case

    then please tell me where i should put the part out of the main

  13. #38
    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

    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)

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

    Default Re: case

    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.

  15. #40
    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

    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)

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

    Default Re: case

    so, move it to right after the main? right before the switch statement?

    --- Update ---

    inbetween those right?

  17. #42
    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

    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)

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

    Default Re: case

    speak english please, i am not understanding what u said

  19. #44
    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

    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)

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

    Default Re: case

    Quote Originally Posted by Ada Lovelace View Post
    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

    this is what i am not really understanding

  21. #46
    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

    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)

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

    Default Re: case

    thanks for helping me understand, i really appreciate it

    btw - i will message again if i need anymore help

  23. #48
    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

    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)

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

    Default Re: case

    Quote Originally Posted by lucas29 View Post
    here is the new code and problem:

    import java.util.scanner;
    {
    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)
    public static void main (String[] args)
    {
    {
    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
    this is the new code and it says that it can't return a value from method whos type is void, on all the seven, error and al the rest in the switch statements

  25. #50
    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

    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)

Page 2 of 3 FirstFirst 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