Hi, I am trying to do a hotel system where i have written up this code, however i was wondering if the there was a way to make the main program simple. So what im trying to do is the code that ‘Views All rooms’ and ‘Adds customer to room’,…
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.
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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]);
}
}}
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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.
riahc3
1,304 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
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;
}
}
}
}
}
riahc3
1,304 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
BTW, thats just "working" code. I have no idea what you want to acomplish.
riahc3
1,304 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
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.
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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.
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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)
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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)
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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;
}
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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;
}
}
}
}
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0