Results 1 to 5 of 5
  1. #1
    Isshin is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default Exiting a while loop using a String?

    I'm trying to make the program read in a list of name and ages (using JOptionPane). It's supposed to quit the loop when the name entered is "quit".

    I learned to use ".equals" instead of "==" or "!=", but now when I run the program, it doesn't look like it's doing anything. I'm not sure what to do.

    Also i'm trying to assign a name to a new name if the age is greater than the last one entered. I'm not quite sure if the way i have it set up will work.

    Any help will be greatly appreciated.

    ----------------------------------------------------------------------
    Java Code:
    import javax.swing.JOptionPane;
    public class Perrine_Oldest {
    public static void main(String[] args){
        
        int l, m, n, o, p, q, r, s, t, newage;
        String Quit, quit, rawr, name, age, x, z, a, b, c;
        name = "rawr";
        l = 1;
        
        while(!name.equalsIgnoreCase("quit"));{
            name = JOptionPane.showInputDialog(null, "Enter the name of person " + l + ":");
            age = JOptionPane.showInputDialog(null, "Enter the age of person " + l + ":");
                n = Integer.parseInt( age );
                rawr = "sigh";
    
                if (n >= n){
                    name = rawr;}
    
                
           l = l + 1;}
        
        
        JOptionPane.showMessageDialog(null, "The oldest person is " + rawr + ".");
        
        System.exit(0);
    }}
    Moderator Edit: code tags added so that the code will retain its formatting and be more readable
    Last edited by Fubarable; 02-21-2010 at 11:13 PM. Reason: Code tags added

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    while(!name.equalsIgnoreCase("quit"));

    try removing the semi colon from in front of the last bracket.

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

    Default

    Nice pick up there Mr. Marshy!

    Indeed that semicolon is going to mess up the program and in fact freeze it. Also I'm not sure that the System.exit(0) should be where it's located (and in fact it's not needed in this program at all).

  4. #4
    Isshin is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Haha, yes, the semicolon was indeed messing me up. Thank you for catching that (i was watching that closely in my first program and didn't even realize it today.)

    The loop now runs as i would like it, but, won't quit if i type in "quit" as the name.

    The system.exit(0) is just habit from going to a lab for the class.

    Also, i'll keep the code tags in mind from now on, sorry!

    ---

    Edit: I changed it to "while(!"quit".equals(name))" and it now exits properly.
    Last edited by Isshin; 02-21-2010 at 11:33 PM.

  5. #5
    Isshin is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    and done. Thanks for catching that ; in the beginning, it let me finish :D

    ----------------

    Java Code:
    		import javax.swing.JOptionPane;
    public class Oldest {
    public static void main(String[] args){
    	
    	int l, n, m;
    	String  rawr, name, age;
    	name = "rawr";
    	rawr = null;
    	l = 1;
    	m = 0;
    	
    	while(!"quit".equalsIgnoreCase(name)){
    		name = JOptionPane.showInputDialog(null, "Enter the name of person " + l + ":");
    		
    		if (!"quit".equalsIgnoreCase(name)){
    		    age  = JOptionPane.showInputDialog(null, "Enter the age of person " + l + ":");
    			n    = Integer.parseInt( age );
    			 
    		if (n > m){
    			m    = n;
    			rawr = name;
    		if (n > m){
    			rawr = name;
    			}}}
    
    		l = l + 1;}
    	
    	JOptionPane.showMessageDialog(null, "The oldest person is " + rawr + ".");
    	
    	System.exit(0);
    }}
    Last edited by Isshin; 02-22-2010 at 03:48 AM.

Similar Threads

  1. exiting a while loop with a string
    By Aldius in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 11:38 PM
  2. Exiting on menuSelected action
    By benra in forum New To Java
    Replies: 8
    Last Post: 10-13-2009, 05:58 PM
  3. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 01:32 PM
  4. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 07:16 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 12:23 AM

Posting Permissions

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