| | |
Need help with the java code!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi there guys!
Can someone help me with the part add tour,when I run it, its always print the same line 2 times and then when I enter the data for guide tour which should be store in the memory, but when using the display method it didn't appear like i want.
And 1 more thing that how can i save the data in array into a file?
I attached the rest of the class so if someone want to run it.
Can someone help me with the part add tour,when I run it, its always print the same line 2 times and then when I enter the data for guide tour which should be store in the memory, but when using the display method it didn't appear like i want.
java Syntax (Toggle Plain Text)
package Solution; import java.io.*; import java.util.*; public class TourBookingSystem { private static final Scanner console = new Scanner (System.in); private static Tour [] tours = new Tour[10]; private static EquipmentHire [] eHires = new EquipmentHire[50]; private static int tourCount = 0; private static int hireCount = 0; public static void main (String [] args) { char selection; loadData(); do { printMenu(); selection = readChar(); selection = Character.toUpperCase(selection); switch (selection) { case 'A': addTour(); break; case 'B': listTours(); break; case 'C': addTourBooking(); break; case 'D': listTourBookings(); break; case 'E': hireEquipment(); break; case 'F': listEquipmentHires(); break; case 'G': cancelBooking(); break; case 'X': System.out.println("Writing data out to file"); saveData(); default: System.out.println("Error - invalid option selected!"); } System.out.println(); } while (selection != 'X'); System.out.println("Program Terminating - goodbye!"); System.out.println(); } public static void printMenu() { System.out.println("****** Tour Booking System Menu ******\n"); System.out.println(" A - Add Tour"); System.out.println(" B - List All Available Tours"); System.out.println(" C - Add Tour Booking"); System.out.println(" D - List All Tour Bookings"); System.out.println(" E - Add Equipment Hire Booking"); System.out.println(" F - List All Equipment Hire Bookings"); System.out.println(" G - Cancel Tour Booking"); System.out.println(" X - Exit"); System.out.println(); System.out.print("Enter your selection: "); } // this method can be used to read any single-character input values // that your program requires from the keyboard public static char readChar() { String input; do { try { input = console.nextLine(); if (input.length() != 1) throw new Exception(); return Character.toUpperCase(input.charAt(0)); } catch (Exception e) { System.out.println("Input must be a single character only - " + "please try again."); } } while (true); } // this method can be used to read any integer values that // your program requires from the keyboard public static int readInt() { int input; do { try { input = console.nextInt(); // clear trailing newline from buffer if (console.hasNextLine()) console.nextLine(); return input; } catch (InputMismatchException e) { System.out.println("Input must be a integer value - " + "please try again."); System.out.println(); // clear erroneous input from buffer if (console.hasNextLine()) console.nextLine(); } } while (true); } public static double readDouble() { double input; do { try { input = console.nextDouble(); // clear trailing newline from buffer if (console.hasNextLine()) console.nextLine(); return input; } catch (InputMismatchException e) { System.out.println("Input must be a double value - " + "please try again."); System.out.println(); // clear erroneous input from buffer if (console.hasNextLine()) console.nextLine(); } } while (true); } public static void addTour() { System.out.println("Add Tour Option Selected"); { Tour temp; String type; String tourID, tourDesc, date, guide; double tourFee; int size; if (tourCount == tours.length) { System.out.println("Error - account list is full!"); return; } System.out.print("Enter Tour ID: "); tourID = console.nextLine(); System.out.print("Enter Tour Description: "); tourDesc = console.nextLine(); System.out.print("Enter Tour Fee: "); tourFee = console.nextDouble(); do { System.out.print("Enter Tour Type " + "(G for guide tour, U for no guide tour): "); type = console.nextLine().toUpperCase(); System.out.println(); } while (type.equals("G") == false && type.equals("U") == false); if (type.equals("U")) temp = new Tour(tourID, tourDesc, tourFee); else { System.out.print("Enter tour date: "); date = console.nextLine(); System.out.print("Enter tour size: "); size = console.nextInt(); System.out.print("Enter tour guide name: "); guide = console.nextLine(); temp = new GuidedTour(tourID, tourDesc, tourFee, date, size, guide); } // clear the trailing newline left in the input buffer if (console.hasNextLine()) console.nextLine(); // add the new object into the next empty spot in the array tours[tourCount] = temp; tourCount++; } } public static void listTours() { System.out.println("List All Tours Option Selected"); if (tourCount == 0) { System.out.println("Tour List is Empty!"); } else { for (int i = 0; i < tourCount; i++) { tours[i].display(); System.out.println(); } } } public static void addTourBooking() { } public static void listTourBookings() { } public static void hireEquipment() { } public static void listEquipmentHires() { } public static void cancelBooking() { } public static void loadData() { } public static void saveData() { } }
And 1 more thing that how can i save the data in array into a file?
I attached the rest of the class so if someone want to run it.
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
I've not checked if Alex is correct, but as per your question regarding the storage of your array information. It can so easily be done by writing the entire array object to a file.
The is a ObjectOutputStream class and if you check out the API you'd see an example of how this would be done. Alternatively you can write the contents of the array in a text file using a looping structure that reads each element of the array. You can then read the contents back with the Scanner class.
The is a ObjectOutputStream class and if you check out the API you'd see an example of how this would be done. Alternatively you can write the contents of the array in a text file using a looping structure that reads each element of the array. You can then read the contents back with the Scanner class.
•
•
•
•
I've not checked if Alex is correct, but as per your question regarding the storage of your array information. It can so easily be done by writing the entire array object to a file.
The is a ObjectOutputStream class and if you check out the API you'd see an example of how this would be done. Alternatively you can write the contents of the array in a text file using a looping structure that reads each element of the array. You can then read the contents back with the Scanner class.
I was trying to figure out if it was the do-while loop repeating or if it was the information on the screen reprinted and it's obviously not the 2nd option. My apologies.
Last edited by Alex Edwards; Jun 4th, 2008 at 4:11 pm.
Java Syntax (Toggle Plain Text)
do { System.out.print("Enter Tour Type (G for guide tour, U for no guide tour): "); String temp2 = console.nextLine();//apparantly there was still data in the stream that needed to be flushed? type = console.nextLine().toUpperCase(); System.out.print(""); } while (!type.equalsIgnoreCase("G") && !type.equalsIgnoreCase("U"));
Sorry for the multi-posts, unfortunately you can't edit your previous post after 30 minutes.
![]() |
Similar Threads
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Auto generate Java Code from WSDL using Axis (Java)
- Executing Shell script from Java code. (Java)
- download jar files using java code (Java)
- I want to know about the java code. Help please!!!! (Java)
- Move out java code from jsp (Java)
- Help with Java code (Java)
- Can N E 1 Help!!!! -- Need sample Java code (Java)
- java code newbie (Java)
Other Threads in the Java Forum
- Previous Thread: SQL Insert - which method to use
- Next Thread: How to draw a graph in Java applet
| Thread Tools | Search this Thread |
Tag cloud for Java
addball android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time tree trolltech utility windows





