1.11M Members

make my program simple

 
0
 

you missed part of it.

line 28 change char to inputChar

also you only need one scanner just call the function you need from the one also remove the public static and move the scanner creation inside of the main method.

your menu is requesting integers as a way of selecting different options if you wnat to do that instead of characters replace the 'A' with just 1 and the 'V' with just 2 . . .

i copied your code and corrected a few things the rest is for you to fill in:

switch(inputChar)  //so compair the character you read the ones provided for each case
    case 'A':
    addGuest();  //all the code that was here is replaced by this line which points to the code you had here
    break;
    case 'V':
    viewRooms();
    break;
    //. . . ect
    default:
    //if you want to do something when the user enteres an invalid character put it here.
    break;
}
public void addGuest(){  //moved all your code inside of case A down here.
    //Add Guest
    sc.nextLine();
    System.out.print("Please enter your guest: ");
    String guest=sc.nextLine();
    names=addName(guest,names);
    System.out.print("Room: ");
    int room=sc.nextInt();
    rooms=addRoom(room,rooms);
    //sort rooms by ascending... don't use arrays.sort!
    for(int x=0;x<rooms.length;x++)
    {
        for(int y=0;y<rooms.length;y++)
        {
            if(rooms[x]<rooms[y])
            {
                int dummy=rooms[x];
                String dummy2=names[x];
                rooms[x]=rooms[y];
                names[x]=names[y];
                rooms[y]=dummy;
                names[y]=dummy2;
            }
        }
    }
}
public void viewRooms(){
    //code to view rooms
}
//more functions go down here

Edit: almost forgot if you decide to chage to using integers instead of characters make sure you change the data type of inputChar to int and read an int with the scanner instead of a char like you are now.

hope i answered all your questions.

if not ill be back in a few hours and answer any more you have.

 
0
 
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:37)
Java Result: 1

I HAVE EDITIED AS ABOVE BUT I AM STILL GETTING ERRORS ALOT MORE THIS TIME.

 
0
 

The string variables arent being used in the switch method. I have tried to move it but not still cannot get it working.

 public static Scanner sc = new Scanner(System.in);
   Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO code application logic here

 String names[]=new String[0];
        int rooms[]=new int[0]; 
   while(true)
   {
                int choice=0;
                System.out.println("\n\nMENU ");//menu
                System.out.println("1) Add Guest");
                System.out.println("2) View All Rooms");
                System.out.println("3) Display Empty Rooms");
                System.out.println("4) Find Room From Customer Name");
                System.out.println("5) Store Program data into file");
                                System.out.println("6) Load Program from data file");
                                System.out.println("7) View Rooms Alphabetically");
                System.out.print("Choice: ");
                choice=sc.nextInt();
                if(choice==10||choice ==0)//either or
                    break;

                              inputChar = scan.nextChar();
                              switch(inputChar)
                                case 'A':
                                addGuest();
                                break;
                               case 'V':
                               viewRooms();
                                break;
                               default:
                                   break;
                              }}}
                              public void addGuest(){
                        //Add Guest
                        sc.nextLine();
                        System.out.print("Please enter your guest: ");
                        String guest=sc.nextLine();
                        names=addName(guest,names);
                        System.out.print("Room: ");
                        int room=sc.nextInt();
                        rooms=addRoom(room,rooms);
                        //sort rooms by ascending... don't use arrays.sort!
                        for(int x=0;x<rooms.length;x++)
                        {
                            for(int y=0;y<rooms.length;y++)
                            {
                                if(rooms[x]<rooms[y])
                                {
                                    int dummy=rooms[x];
                                    String dummy2=names[x];
                                    rooms[x]=rooms[y];
                                    names[x]=names[y];
                                    rooms[y]=dummy;
                                    names[y]=dummy2;
                                }
                            }
                        }
                              }

                     public void viewRooms(){
                    {
                        //List All Guests and Room Numbers
                        System.out.println("ROOM\t\t\tGUEST");
                        for(int x=0;x<rooms.length;x++)
                        {
                            System.out.println(rooms[x]+"\t\t\t"+ names[x]);
                        }

                                        }}
 
0
 

here is your problem you are scanning when there is nothing to scan. you need to choose to do your menu options with either numbers or characters.

line 1: delete

line 2: move inside of main (like i said above)

line 21: your reading an int as the user's selection then on line 25 your reading the consol again for the user's selection. Since you already read it there is nothing to read in consol. This is what i was talking about earlier when i said you need to pick to use characters or integers. (note: you can use the character version of the numbers if you want to use v and other characters.)

--also change all sc to scan.

line 26: your missing a brace after the switch(inputChar) (sorry about that i made a typo above when i told you how to write the switch)

also where is your class declairation? e.g. public class HotelProgram {
this should come right after your import statements.

If you dont understand any part of this please say so i will explain it.

 
0
 

Im downloading the Java 7 JDK.....

Just like others have said, we are trying to help you but we have no ideas what your main goal is and/or what you currently know.

 
0
 

Work from this:

package tests;

import java.util.Scanner;

public class Tests 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        String names[]=new String[0];
        int rooms[]=new int[0];

        do
        {
             int choice=0;
             System.out.println("\n\nMENU ");//menu
             System.out.println("1) Add Guest");
             System.out.println("2) View All Rooms");
             System.out.println("3) Display Empty Rooms");
             System.out.println("4) Find Room From Customer Name");
             System.out.println("5) Store Program data into file");
             System.out.println("6) Load Program from data file");
             System.out.println("7) View Rooms Alphabetically");
             System.out.print("Choice: ");
             choice=scan.nextInt();
             if((choice==10) || (choice ==0))//either or
             {
                 break;
             }
             char inputChar = (char)scan.nextLine().charAt(0); //No idea what you are trying to do here
             switch(inputChar)
             {
                case 'A':
                 addGuest(names);
                 break;
                 case 'V':
                 viewRooms();
                 break;
                 default:
                 break;
             }


        }while(true);


    }


     public static void addGuest(String[] names)
     {
         //Add Guest
         Scanner sc = new Scanner(System.in);
         //sc.nextLine();
         System.out.print("Please enter your guest: ");
         String guest=sc.nextLine();
         names=addName(guest,names);
         System.out.print("Room: ");
         int room=sc.nextInt();
         rooms=addRoom(room,rooms);
         //sort rooms by ascending... don't use arrays.sort!
         for(int x=0;x<rooms.length;x++)
         {
             for(int y=0;y<rooms.length;y++)
             {
                 if(rooms[x]<rooms[y])
                 {
                     int dummy=rooms[x];
                     String dummy2=names[x];
                     rooms[x]=rooms[y];
                     names[x]=names[y];
                     rooms[y]=dummy;
                     names[y]=dummy2;
                 }
             }
         }
      }



}
 
0
 

BTW, thats just "working" code. I have no idea what you want to acomplish.

 
0
 

MY AIM IS USING THE FIRST CODE I POSTED TO EDIT THAT TO DO THE FOLLOWING..

i want to put the code that ‘Views All rooms’ and ‘Adds customer to room’,
into separate Methods just.

MENU SYSTEM
I want a menu system so the user can select what they want using the menu. for example what i have thought of doing is when u
Enter an ‘A’ to it will run the method add a customer to a room,
‘V’ to view all rooms
E: Display Empty rooms,
D: Delete customer from room,
F: Find room from customer name,
S: Store program data in to file,
L: Load program data from file.
O: View rooms Ordered alphabetically by name.

The menu system is all based on entering the letter to run the methods, i dont want anything to do with number seletion.

I am a new to java and so i will know the basics not alot doe. I have been going through various sites to find tutorials to understand evrything.

 
0
 

I dont understand this bit of code which is on line 21. did change the int to char then nextChar but there is an error. :/

line 21: your reading an int as the user's selection then on line 25 your reading the consol again for the user's selection. Since you already read it there is nothing to read in consol. This is what i was talking about earlier when i said you need to pick to use characters or integers.

 
0
 

since you want to read a character not an integer correct your menu to match and only read one letter from the consol right now your reading an integer at line 29 and a char at line 34
here is the correction based on what you asked. I added comments to all the lines that you appear to be having trouble with.

do
  {
    char choice;                                //i changed choice from int type to char type
    System.out.println("\n\nMENU ");//menu
    System.out.println("A) Add Guest");
    System.out.println("V) View All Rooms");
    System.out.println("D) Display Empty Rooms");
    System.out.println("F) Find Room From Customer Name");
    System.out.println("S) Store Program data into file");
    System.out.println("L) Load Program from data file");
    System.out.println("R) View Rooms Alphabetically");
    System.out.println("E) Exit program");
    System.out.print("Choice: ");
    choice=scan.nextChar();  //read the users selection and store it in choice
    if(choice=='E')          //if the selection is E exit the loop
    {
      break;
    }
    switch(Choice)           //compairs the value placed in paranthesis to the value of each case
                             //until it finds a match.  If it does not find a match it will go to 
                             //default case.  If there is no default case it exits the switch statement. 
    {
      case 'A':          //If the letter inputed equals A call addGuest();
        addGuest(names); //Calls add guest
      break;
      case 'V':          //If the letter inputed equals V call viewRooms()
        viewRooms();     //Calls viewRooms()
      break;
      //add cases for other leters here
      default:
      break;
    }
  }while(true);
 
0
 

I am now recieveing this error. I updated evrything.
** choice=scan.nextchar(); ** that bit code just always comes up red

MENU 
A) Add Guest
V) View All Rooms
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Scanner.nextchar
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:33)
R) View Rooms Alphabetically
E) Exit program
Choice: Choice: Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
 
0
 
String names[]=new String[0];
        int rooms[]=new int[0]; 

The code above says the variable is not in use do i need to move this somewhere? I have used the variables in the method coding

 
0
 

sorry about that nextChar method does not exist
do it like you were doing earlier:

char choice = (char)scan.nextLine().charAt(0);

 
0
 

How come the variables are not in use..

Also the word choice does it have to have a capitcal C through out?

MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: Choice: A
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable choice might not have been initialized
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:34)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
 
0
 

variables are cases sensitive meaning they have to match exactly for java to think they are the same thing.

Standard is to make variables camelCase first word lowercase and all following words uppercase

example: thisIsCamelCase

 
0
 

I have fixed that one but most of my code is all red underlined.. :(

MENU 
A) Add Guest
V) View All Rooms
D) Display Empty Rooms
F) Find Room From Customer Name
S) Store Program data into file
L) Load Program from data file
R) View Rooms Alphabetically
E) Exit program
Choice: Choice: A
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: addGuest
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:42)
Java Result: 1
 
0
 

well for starters whats on line 42?

also check what netbeans tells you by hovering your mouse over the caution symbols on the left of your code.

 
0
 

**line 42 has addGuest();
**

  char Choice = (char)scan.nextLine().charAt(0);                        
                        if(Choice=='E')          //if the selection is E exit the loop

                              break;

                                  switch(Choice)
                                          {

                                      case 'A':
                                addGuest();
                                break;
                               case 'V':
                               viewRooms();
                                break;
                               default:
                                   break;
                                  }}}}

                              public void addGuest(){
                        //Add Guest
                        scan.nextLine();
                        System.out.print("Please enter your guest: ");
                        String guest=scan.nextLine();
                        names=addName(guest,names);
                        System.out.print("Room: ");
                        int room=scan.nextInt();
                        rooms=addRoom(room,rooms);
                        //sort rooms by ascending... don't use arrays.sort!
                        for(int x=0;x<rooms.length;x++)
                        {
                            for(int y=0;y<rooms.length;y++)
                            {
                                if(rooms[x]<rooms[y])
                                {
                                    int dummy=rooms[x];
                                    String dummy2=names[x];
                                    rooms[x]=rooms[y];
                                    names[x]=names[y];
                                    rooms[y]=dummy;
                                    names[y]=dummy2;
}}}




                     public void viewRooms(){
                    {
                        //List All Guests and Room Numbers
                        System.out.println("ROOM\t\t\tGUEST");
                        for(int x=0;x<rooms.length;x++)
                        {
                            System.out.println(rooms[x]+"\t\t\t"+ names[x]);
                        }

                                        }}

    public static String[] addName(String guest, String names[])
    {
        String x[]=new String[(names.length)+1];
        System.arraycopy(names,0,x,0,names.length);//System.arraycopy(first array, starting point, second array, start point, how much
        //add something for maybe if the person is already here, but cased differently??
        for(int a=0;a<x.length;a++)
        {
            if(guest.equalsIgnoreCase(x[a]))
            {
                System.out.println("This guest has already checked in.");
                System.out.print("New Guest: ");
                guest=scan.nextLine();
            }
        }
        x[x.length-1]=guest;
        return x;   
    }

    public static int[] addRoom(int room, int rooms[])
    {
        int r[]=new int[(rooms.length)+1];
        System.arraycopy(rooms,0,r,0,rooms.length);//System.arraycopy(first array, starting point, second array, start point, how much
        for(int x=0;x<r.length;x++)
        {
            if(r[x]==room)
            {
                System.out.println("ROOM ALREADY OCCUPIED!");
                System.out.print("Reenter room: ");
                room=scan.nextInt();
            }
        }
        r[r.length-1]=room;
        return r;
}
 
0
 

i corrected all the problems i could find the program now works for the first two choices. the problems are listed in the comments the first time they show all proceeding instances are uncommented.

This is a great little program i hope you continue to improve your skills.

A couple things you should do before working on this any more:
1. read about scope in java
2. read about static vs non static

package tests;

import java.util.Scanner;

public class Test {
    static String names[]=new String[0]; 
    static int rooms[]=new int[0];      //had to move these outside of main to make them accessable to your methods.

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);

        do{
            char choice;
            System.out.println("\n\nMENU ");//menu
            System.out.println("A) Add Guest");
            System.out.println("V) View All Rooms");
            System.out.println("D) Display Empty Rooms");
            System.out.println("F) Find Room From Customer Name");
            System.out.println("S) Store Program data into file");
            System.out.println("L) Load Program from data file");
            System.out.println("R) View Rooms Alphabetically");
            System.out.println("E) Exit program");
            System.out.print("Choice: ");

            char Choice = (char)scan.nextLine().charAt(0);

            if(Choice=='E')
                break;

            switch(Choice){
                case 'A':
                    addGuest();  //you were calling a non static method from main here.  that not allowed
                    break;
                case 'V':
                    viewRooms();
                    break;
                default:
                    break;
             }

        }while(true);
    }

    public static void addGuest(){  //if you dont put methodds in a seperate class and instance an object they have to be static to use inside of main.
        Scanner scan = new Scanner(System.in);  //variable scope is importaint since the previous scanner is in main you need to create a new one inside of all of your methods that are outside of main.
        scan.nextLine();

        System.out.print("Please enter your guest: ");
        String guest=scan.nextLine();

        names=addName(guest,names);
        System.out.print("Room: ");

        int room=scan.nextInt();
        rooms=addRoom(room,rooms);
        //sort rooms by ascending... don't use arrays.sort!

        for(int x=0;x<rooms.length;x++){
            for(int y=0;y<rooms.length;y++){
                if(rooms[x]<rooms[y]){
                    int dummy=rooms[x];
                    String dummy2=names[x];
                    rooms[x]=rooms[y];
                    names[x]=names[y];
                    rooms[y]=dummy;
                    names[y]=dummy2;
                }
            }
        }
    }

    public static void viewRooms(){
        System.out.println("ROOM\t\t\tGUEST");
        for(int x=0;x<rooms.length;x++){
            System.out.println(rooms[x]+"\t\t\t"+ names[x]);
        }
    }

    public static String[] addName(String guest, String names[]){
        Scanner scan = new Scanner(System.in);
        String x[]=new String[(names.length)+1];
        System.arraycopy(names,0,x,0,names.length);

        for(int a=0;a<x.length;a++){
            if(guest.equalsIgnoreCase(x[a])){
                System.out.println("This guest has already checked in.");
                System.out.print("New Guest: ");
                guest=scan.nextLine();
            }
        }

        x[x.length-1]=guest;
        return x;   
    }

    public static int[] addRoom(int room, int rooms[]){
        Scanner scan = new Scanner(System.in);
        int r[]=new int[(rooms.length)+1];
        System.arraycopy(rooms,0,r,0,rooms.length);

        for(int x=0;x<r.length;x++){
            if(r[x]==room){
                System.out.println("ROOM ALREADY OCCUPIED!");
                System.out.print("Reenter room: ");
                room=scan.nextInt();
            }
        }

        r[r.length-1]=room;
        return r;
    }
}
 
0
 

i will start reading in to what u have suggested asap..

for now i still have a error.. when i take static out the error goes but doesnt run.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable names cannot be referenced from a static context
    at hotelprogram.Hotelprogram.addGuest(Hotelprogram.java:64)
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:44)
Java Result: 1






                              public static void addGuest(){

                              Scanner scan = new Scanner(System.in);
                        //Add Guest
                        scan.nextLine();

      System.out.print("Please enter your guest: ");
        String guest=scan.nextLine();
        names=addName(guest,names);
        System.out.print("Room: ");
        int room=scan.nextInt();
        rooms=addRoom(room,rooms);
        //sort rooms by ascending... don't use arrays.sort!
        for(int x=0;x<rooms.length;x++){
            for(int y=0;y<rooms.length;y++){
                if(rooms[x]<rooms[y]){
                    int dummy=rooms[x];
                    String dummy2=names[x];
                    rooms[x]=rooms[y];
                    names[x]=names[y];
                    rooms[y]=dummy;
                    names[y]=dummy2;
                }
            }
        }
    }
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article