Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JOptionPane menu and multiple methods

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JOptionPane menu and multiple methods

    //Now bare with me, I'm not the best coder. What I'm trying to do in my coding is create a JOptionPane web. That is what I'm having trouble with, my menus don't quite work right yet. If you could help me or //lead me in the right direction, it would be very much appreciated.


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.lang.NumberFormatException;
    import java.util.LinkedList;
     
    public class StudentTester {
     
    	public static void main(String[] args) throws FileNotFoundException, NumberFormatException {
    		//for (;;) {
    			Scanner in = new Scanner(System.in);
    			JOptionPane mainmenu = new JOptionPane();
     
    			//Scanner input = new Scanner(System.in);
     
    			LinkedList<Student> students = new LinkedList<Student>();
     
    			boolean isTrue = false;
    			while (isTrue == false)
    			{
     
    			int num = 0;
     
    			while (num != 6) {
     
     
    					int input1 = Integer.parseInt(mainmenu.showInputDialog(null,"1 Please type the path to the .txt you wish to import\n2  How would you like to search for your record?\n3  Insert the Students Name\n4  Remove a Student\n5  How would you like to order your report?\n6  View the updated records\n7  Exit\n"));
     
    					num = input1;
     
     
     
     
    				if(num == 1){
     
    					JOptionPane importFile = new JOptionPane();
     
    					  Scanner filein= new Scanner(System.in);
                          importFile.showInputDialog(null,"Input File: ");
    				      String inputFileName = filein.next();
     
                          System.out.println("Hi");
     
    				      File inputFile = new File(inputFileName);
    				      Scanner infile = new Scanner(inputFile);
     
     
    					while (infile.hasNext()) {
    						students.add(new Student(infile.nextInt(), infile.next(), infile
    								.nextDouble()));					
    						infile.close();
     
    					}
    				}
     
     
     
     
     
     
    					else if (num == 2){
    					int num1 = 0;
    					JOptionPane searchType = new JOptionPane();
    					while (num1 != 4) {
    						int input2 = Integer.parseInt(searchType.showInputDialog(null,"1 Name\n2 ID\n3 GPA\n4 Back\n"));
    						num1 = input2;
     
     
     
    						if (num1== 1){
    							JOptionPane lastName = new JOptionPane();// student's
    																	// name
    																	// search
    							lastName.showInputDialog(null, "Student Name: ");
    							String name = "";
     
     
    							boolean found = false;
    							for (Student s : students) {
    								if (s.getLastName().equals(lastName))
    									found = true;
     
    							}
    							if (found)
    								JOptionPane.showMessageDialog(null,"A student was found");
    							else
    								JOptionPane.showMessageDialog(null,"A student was not found");
    						}
     
     
     
    							else if (num1==2){
    							JOptionPane sID = new JOptionPane();// student's ID
    																// search
    							sID.showInputDialog(null, "ID");
    							int id = Integer.parseInt(JOptionPane.showInputDialog(null,
    									"What in the ID of the Student you are looking for?"));
    							String result = "";
     
    							for (Student s : students) {
    								if (s.getId() == id) {
    									result += s;
    								}
     
    							}
     
    						}
     
     
    							else if (num1==3){
    							JOptionPane sGPA = new JOptionPane();// students"
    																	// GPA
    																	// search
    							sGPA.showInputDialog(null, "GPA");
    						}
     
    							else if (num1==4){
    							JOptionPane sMes = new JOptionPane();// back send
    																	// back to
    						}									// main menu
     
    							else{
    							JOptionPane.showMessageDialog(null,
    									"Invalid Choice");
    							}
    					}
    					}
     
     
     
     
     
    			else if (num == 3){
    					String fullName = "";
    					String number11 = "";
    					String number22 = "";
    					int number1 = 0;
    					double number2 = 0;
    					JOptionPane addStudent = new JOptionPane();
    					fullName = addStudent.showInputDialog(null, "Name: ");
     
    					number11 = addStudent.showInputDialog(null, "ID: ");
    					number22 = addStudent.showInputDialog(null, "GPA: ");
    					number1 = Integer.parseInt(number11);
    					number2 = Double.parseDouble(number22);
     
    			}
     
     
    			else if (num == 4){
    					JOptionPane rStudent = new JOptionPane();
    					rStudent.showInputDialog(null, "Name");
    					students.remove("");
    			}
     
     
    			else if (num == 5){
    					String output = "";
    					JOptionPane outReport = new JOptionPane();
    					//output = outReport.showInputDialog("Enter output name");
    					//PrintWriter out = new PrintWriter(output);
    			}
     
     
    			else if (num == 6){
    					JOptionPane vRoster = new JOptionPane();
    					vRoster.showMessageDialog(null, students);
    			}
     
    			else if (num == 7){
    					JOptionPane mesDefault = new JOptionPane();
    					mesDefault.showMessageDialog(null, "Exited");
    					System.exit(0);
    			}
    			else{
    					JOptionPane.showMessageDialog(null, "Invalid Choice");
     
     
    			}
    			}
    			}
    			}
     
     
    	}


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane menu and multiple methods

    I guess, I didn't clarify enough for an answer?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,376
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: JOptionPane menu and multiple methods

    my menus don't quite work right
    Describe what happens and
    Please explain what your problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane menu and multiple methods

    ok here is my update code so far, My problem is I'm having trouble inputing a text file into my LinkedList through JOptionPane. I want to be able to type the direct path, but I keep getting errors. Here is the error message: Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at StudentTester.main(StudentTester.java:51)

    update code: Don't mind commented out sections, I just did that for I can focus on one part at a time

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.lang.NumberFormatException;
    import java.util.LinkedList;
     
    public class StudentTester {
     
    	public static void main(String[] args) throws  NumberFormatException {
    		//for (;;) {
    			//Scanner in = new Scanner(System.in);
    		//	JOptionPane mainmenu = new JOptionPane();
     
    			//Scanner input = new Scanner(System.in);
     
     
    			LinkedList<Student> students = new LinkedList<Student>();
    			boolean isTrue = false;
    			while (isTrue == false)
    			{
     
    			int num = 0;
     
    			while (num != 6) {
     
     
    					int input1 = Integer.parseInt(JOptionPane.showInputDialog(null,"1 Please type the path to the .txt you wish to import\n2  How would you like to search for your record?\n3  Insert the Students Name\n4  Remove a Student\n5  How would you like to order your report?\n6  View the updated records\n7  Exit\n"));
     
    					num = input1;
     
     
     
     
    				if(num == 1){
     
    					//JOptionPane importFile = new JOptionPane();
     
    					 System.out.println("Hi");
     
                         String inputFileName = JOptionPane.showInputDialog(null,"Input File: ");
     
     
     
     
    				      File inputFile = new File(inputFileName);
    				   try{   
    					   Scanner in = new Scanner(inputFile);
    				   while (in.hasNext()) {
    						students.add(new Student(in.nextInt(), in.next(), in
    								.nextDouble()));					
    						in.close();
    				   }
    }
    catch(FileNotFoundException e){
    	System.out.println("File Not Found");
    }
     
     
     
     
    					}
    			}
    			}
    	}
    }
     
     
     
    					/*else if (num == 2){
    					int num1 = 0;
     
    					while (num1 != 4) {
    						int input2 = Integer.parseInt(JOptionPane.showInputDialog(null,"1 Name\n2 ID\n3 GPA\n4 Back\n"));
    						num1 = input2;
     
     
     
    						if (num1== 1){
    							// student's
    																	// name
    																	// search
    							JOptionPane.showInputDialog(null, "Student Name: ");
    							String name = "";
     
     
    							boolean found = false;
    							for (Student s : students) {
    								if (s.getLastName().equals(name))
    									found = true;
     
    							}
    							if (found)
    								JOptionPane.showMessageDialog(null,"A student was found");
    							else
    								JOptionPane.showMessageDialog(null,"A student was not found");
    						}
     
     
     
    							else if (num1==2){
    							// student's ID
    																// search
     
    							int id = Integer.parseInt(JOptionPane.showInputDialog(null,
    									"What in the ID?"));
    							String result = "";
     
    							for (Student s : students) {
    								if (s.getId() == id) {
    									result += s;
    								}
     
    							}
     
    						}
     
     
    							else if (num1==3){
    								int gpa = Integer.parseInt(JOptionPane.showInputDialog(null,
    										"What in the GPA?"));
     
    							String result = "";
    							for (Student s : students) {
    								if (s.getGpa() == gpa) {
    									result += s;										// GPA
    																	// search
     
    						}
     
    							else if (num1==4){
    							JOptionPane sMes = new JOptionPane();// back send
    																	// back to
    						}									// main menu
     
    							else{
    							JOptionPane.showMessageDialog(null,
    									"Invalid Choice");
    							}
    					}
    					}
     
     
     
     
     
    			else if (num == 3){
    					String fullName = "";
    					String number11 = "";
    					String number22 = "";
    					int number1 = 0;
    					double number2 = 0;
     
    					fullName = JOptionPane.showInputDialog(null, "Name: ");					
    					number11 = JOptionPane.showInputDialog(null, "ID: ");
    					number22 = JOptionPane.showInputDialog(null, "GPA: ");
    					number1 = Integer.parseInt(number11);
    					number2 = Double.parseDouble(number22);
     
    			}
     
     
    			else if (num == 4){
     
    					JOptionPane.showInputDialog(null, "Name");
    					students.remove("");
    			}
     
     
    			else if (num == 5){
    					JOptionPane.showMessageDialog(null, students);
    					//output = outReport.showInputDialog("Enter output name");
    					//PrintWriter out = new PrintWriter(output);
    			}
     
     
    			else if (num == 6){
    					JOptionPane vRoster = new JOptionPane();
    					JOptionPane.showMessageDialog(null, students);
    			}
     
    			else if (num == 7){
    					JOptionPane mesDefault = new JOptionPane();
    					JOptionPane.showMessageDialog(null, "Exited");
    					System.exit(0);
    			}
    			else{
    					JOptionPane.showMessageDialog(null, "Invalid Choice");
     
     
    			}
    			}
    			}
    			}
     
     
    			}
    	}
    }
     
     
    	*/

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,376
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: JOptionPane menu and multiple methods

    java.util.InputMismatchException
    ..
    at java.util.Scanner.nextInt(Unknown Source)
    at StudentTester.main(StudentTester.java:51)
    It looks like the program tried to read what you entered and convert it to an int value using nextInt.
    Did you enter something other than a number?
    Either change what you enter to be a number or use a different method (like nextLine) to read in a String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multiple methods in the same inner class?
    By tommyf in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 24th, 2012, 12:10 PM
  2. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  3. [SOLVED] Using Trigonnometric Methods with JOptionPane?
    By Java Neil in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 17th, 2011, 11:17 AM
  4. Completely Lost in this problem(multiple methods)
    By wacarter in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 31st, 2010, 04:44 PM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 08:44 PM