Results 1 to 8 of 8
  1. #1
    borth92 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Calling Methods in switch Statement

    Hopefully this is a quick answer, but I simply cannot get a simple switch statement to call another method...heres my code so far:

    //Note: cases that break are just because I have not completely finished
    Java Code:
    import java.util.*;
    public class HotelSelection
    {
    
       public static void main(String[] args) {
           
           Scanner scan = new Scanner(System.in);
           System.out.println("Please select one of the following choices: 1.Choose a new hotel 2.Reserve a particular room in chosen hotel 3.Reserve any room in chosen hotel 4.Cancel a room reservation in chosen hotel 5.Check vacancies in chosen hotel 6.Quit");
           int choice = scan.nextInt();
           
           switch (choice) {
               case 1: String name;
                       int size;
                      System.out.println("Please select a hotel (spelling must be exact): Holiday Inn / Comfort Inn / Motel 6");
                      name = scan.next();
                      if (name == "HolidayInn") {
                            size = 100; }
                      if (name == "ComfortInn") {
                            size = 150; }
                      if (name == "Motel6") {
                          size = 45; }
                      
               case 2: int roomNumber;
                       System.out.println("Select a room number");
                       roomNumber = scan.nextInt();
                       System.out.println(reserveRoom(int roomNumber));
               
               case 3: break;
               
               case 4: break;
               
               case 5: break;
               
               case 6: break;
               
            }
        }
        
    
       public static void Hotel(String name, int size) {
        int[] name = new int[size];
        }
        
       public boolean reserveRoom(int roomNumber) {
            
            if(name[roomNumber - 1] = null) {
               name[roomNumber - 1] = 1;
               return true;
            }
            else {
                return false;
            }
        }
    }
    Last edited by Eranga; 12-09-2010 at 03:03 AM. Reason: code tags added

  2. #2
    borth92 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    most of my problems are getting name from one switch to a method...and name being an array not a string

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also please use code tags when you are posting code segments next time in the forum. Unformated codes are really hard to read. If you don't know how to do that, check on my forum signature.

  5. #5
    borth92 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default I should be more clear

    the program does things that relate to booking a hotel room. My problem is I do not know how to get variables from case 1's method to work in case 2's method and so on...

  6. #6
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    why dont you just declare the variables outside of the switch statement?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by borth92 View Post
    Java Code:
                      if (name == "HolidayInn") {
                            size = 100; }
                      if (name == "ComfortInn") {
                            size = 150; }
                      if (name == "Motel6") {
                          size = 45; }
    This piece of code is definitely incorrect; use the equals( ... ) method and see all the other threads overhere for the reason.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    I doubt the code you've supplied even compiles:

    System.out.println(reserveRoom(int roomNumber));

    That is plain incorrect.

    Also the lack of a break after case 1 means it will fall through into case 2 as well.

Similar Threads

  1. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 02:22 PM
  2. Demonstration of the switch statement
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  3. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 03:48 AM
  4. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  5. Help with gigantamous switch statement
    By trill in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •