#booking System Error
1 messages · Page 1 of 1 (latest)
So yu trying to make a search index for looking up bookings?
Okay, so i wrote a very simple way to look for names, but you will have to change how it gathers the data, and what the key word is to look for.
System.out.println("What name are you looking for?");
System.out.print("> ");
Scanner input = new Scanner(System.in);//create scanner for user input
String search = input.nextLine(); //save the user input
System.out.println("Okay, searching for: " + search);
List<String> names = new ArrayList<String>();//an array that holds a list of names
try {
Scanner scan = new Scanner(new File("names"));
//read through the file for all the names
while(scan.hasNextLine()) {
names.add(scan.nextLine());
}
//close the scanner
scan.close();
} catch (FileNotFoundException e) {
//handle any errors
e.printStackTrace();
}
List<String> results = new ArrayList<String>();//an array to hold the results
for(int i = 0; i < names.size(); i++) {
//check to see if the name is what the user is looking for or contains something similar to it
if(names.get(i).equals(search) || names.get(i).contains(search)) {
results.add(names.get(i));//if it does, add it to the results
}
}
//print out the results
System.out.println("Results:");
for(int i = 0; i < results.size(); i++) {
System.out.println(results.get(i));
}
input.close();//close the scanner```
This is my way, there is a better way to about this, but this is a basic way to go about it. I don't know which error you have exactly but this is what i got. Hopefully this helps you.
I came so far. It just dosen't work to put a name and find a booking. It automatically print "the name does not match anyone"
Can you send me the .java file that this contains?
Sorry, like do you want me to paste the code?
yeah you can do that, its just hard to see the code with the screenshots.. they're weird for my screen
static void TabortBokning(int[] Platser) {
try{
Scanner Scanner = new Scanner(System.in);
System.out.println("How do you want to cancel the booking (1) dateofbirth (2) name ");
int choice = Scanner.nextInt();
String search;
if(choice==1){
System.out.println("Type date of birth: ");
int dateOfBirth = Scanner.nextInt();
search = String.valueOf(dateOfBirth);
}
else{
System.out.println("type name: ");
String förnamn = Scanner.next();
search=förnamn;
}
boolean hittad=false;
for (int i = 0; i < Platser.length; i++) {
if(Platser[i]!= 0 && (String.valueOf(Platser[i]).equals(search) || String.valueOf(Platser[i]).equalsIgnoreCase(search))){
Platser[i] = 0;
System.out.println("the booking was cancelled");
hittad=true;
break;
}
}
if (!hittad){
System.out.println("the input does not match anyone");
}
}catch(InputMismatchException e){
System.out.println("Vänligen Ange ditt födelsdatum ");
}
}
If you want, I can send the code where I book a seat
I assume the "Plaster" parameter is the bookings array that holds all the bookings correct?
Correct!
okay, can you send me an example of what the array is, so i can debug this and figure it out..
yeah you can do that
@lyric glade
public static void main(String[] args) {
Scanner Scanner = new Scanner(System.in);
int[] Platser = new int[21];
int alternativ=0;
while (alternativ!= 6) {
System.out.println(" Booknings Meny: ");
System.out.println("(1). Boka plats");
System.out.println("(2). Hitta bokning ");
System.out.println("(3). Avboka bokning");
System.out.println("(4). Ange antal lediga platser");
System.out.println("(5). Berakna vinst av salda biljetter");
System.out.println("(6). Booka fönsterplats");
System.out.println("(7). Avsluta program\n");
System.out.println("Vad kan vi hjalpa dig med?");
try{
alternativ = Scanner.nextInt();
if (alternativ < 1 || alternativ > 6) {
System.out.println("OBS! välj nummer endast mellan 1-6 , Vänligen försök igen.\n");
continue;
}
} catch(InputMismatchException e){
System.out.println("[Skriv endast val av nummer]\n");
Scanner.nextLine();
continue;
}
its fine, i got the file
YEs
So, your array "Plater" Is holding the date of birth of the booking. Not the name.. So how you can fix this is by creating a booking class that will store the first name, the last name, the gender and Date of birth and make an array that holds all those Bookings.. Would you like an example of how it would look?
Yes
Okay, give me a minute and ill get it
The last name and gender is unesaccery
Alright!
also i noticed that it was typed in a different language than English. For translation purposes, what language do you type in? so i can write comments and such so you can read
Add coments in english
it's fine
I just have it on swedish because it's a short project for my teacher
Oh okay
Actually no
Okay, well think of it as an array like what you're using but it adds a more flexibility to add functionality. I can try to stick what you got here. If you look up 'Java ArrayList' you can go through the documentation about it.
Alright, it's fine i'll look it up
Okay I got it
great!!
Okay, let me send you both classes.. give me a second.. Mind if i DM them to you?
SUre!!
I also did some improvements to it if you don't mind