Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default Input Validation

    Hey all,

    Just finished up writing a program and my professor requires everything to be coded with input validation. The program has to prompt the user for an answer (A, B, C , D) and I need to display an error message if they input anything but that. Ive done this with numbers easily, but Im not sure how to do this with letters being theres so many letters other than a,b,c,d. Heres what i have:

    Java Code:
    String [] answers = {"B", "D", "A", "A","C", "A", "B", "A", "C", "D", 
    				"B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};
    		
    		String [] studentAnswers = new String[20];
    		
    		
    		for (int i = 0; i < studentAnswers.length; i++ ){
    			
    			studentAnswers[i]= (JOptionPane.showInputDialog("Question " + (i + 1) + ":"));
    		
    				
    
    			
    		}

    Thats whats prompting the user for the answer. It is being set into an array if it makes a difference. Any suggestions would be appreciated :)

    I tried while loop but it didnt work, not sure what went wrong, but Im sure im just being a noob : / dumb book wont teach me it either.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    my professor requires everything to be coded with input validation
    Good for him. This should teach you good habits from the get-go.

    One suggestion: rather than using Strings of one char, why not use chars. You know of course that chars can be used in places where ints can be used.

  3. #3
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Good call, I was actually thinking that today. Ive had this program basically finished friday, and figured validation wasnt gonna be too much of a problem.

    I tried as a test using a do-while statement just to test if i had entered the letter "E"

    }while(studentAnswers[i] = "E");

    and it just went right on to the next question number. What you think?

    Oh and I did try changing to char, but I was kinda hit with how to parse it when using JOptionPane with arrays. Ive scoured the web and tried a few things but I was just stumped. Oh and the professor is a woman haha so "good for her" :)
    Last edited by kickflipper1087; 11-03-2008 at 06:08 AM. Reason: Edited to add the last line.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    }while(studentAnswers[i] = "E");
    Careful here as you're stepping on a few mines in a large minefield.
    1) You never compare anything with a single =. This operator is for assignment, not comparison. For the latter you usually use double "=" like so: ==.
    2) Having said that, you usually don't compare Strings with "==" as this checks the the two objects referred to by the String variables are one and the same, and that's not what we want. Rather we want to know if the two Strings have the same content -- big difference. For this we use the equals method (or equalsIgnoreCase) as in:
    Java Code:
    if (myString1.equals("Fubar")) {

  5. #5
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    AHHH! Im really glad you caught me on that. I even used the correct comparison with two strings in a different part of my program using compareToIgnoreCase, and I dont know why I didnt just think of it here. Okay so I did that with my test case E and it did work, it prompted me again for a valid input. Now its just about the rest of the letters in the alphabet, is there a short way to include them?

  6. #6
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Hey Fubarable,

    I just got it to work, validation is working fine. It just looks extremely extensive. Heres what I did:

    Java Code:
    for (int i = 0; i < studentAnswers.length; i++ ){
    			
    			do{
    			studentAnswers[i]= (JOptionPane.showInputDialog("Question " + (i + 1) + ":"));
    			
    			
    			if (studentAnswers[i].compareToIgnoreCase("A")!= 0 && studentAnswers[i].compareToIgnoreCase("B")!= 0
    					&& studentAnswers[i].compareToIgnoreCase("C")!= 0 && studentAnswers[i].compareToIgnoreCase("D")!= 0)
    				
    			{
    				JOptionPane.showMessageDialog(null, "Not a valid answer. Only answers A, B, C or D are accepted.");
    			}
    			
    			
    			}while(studentAnswers[i].compareToIgnoreCase("A")!= 0 && studentAnswers[i].compareToIgnoreCase("B")!= 0
    					&& studentAnswers[i].compareToIgnoreCase("C")!= 0 && studentAnswers[i].compareToIgnoreCase("D")!= 0);


    }

Similar Threads

  1. TextField Validation
    By Mir in forum AWT / Swing
    Replies: 9
    Last Post: 06-29-2008, 07:28 AM
  2. form validation
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 03-07-2008, 08:04 AM
  3. Swing validation
    By ppayal in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 09:59 AM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 AM
  5. javascript validation
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-14-2007, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •