Results 1 to 5 of 5
- 02-21-2010, 09:54 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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.
----------------------------------------------------------------------
Moderator Edit: code tags added so that the code will retain its formatting and be more readableJava 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); }}Last edited by Fubarable; 02-21-2010 at 10:13 PM. Reason: Code tags added
- 02-21-2010, 10:05 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
while(!name.equalsIgnoreCase("quit"));
try removing the semi colon from in front of the last bracket.
-
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).
- 02-21-2010, 10:25 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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 10:33 PM.
- 02-21-2010, 11:33 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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 02:48 AM.
Similar Threads
-
exiting a while loop with a string
By Aldius in forum New To JavaReplies: 3Last Post: 12-10-2009, 10:38 PM -
Exiting on menuSelected action
By benra in forum New To JavaReplies: 8Last Post: 10-13-2009, 04:58 PM -
String and while loop
By Exception in forum Java AppletsReplies: 5Last Post: 09-24-2009, 12:32 PM -
Using string to terminate loop
By mrblippy in forum New To JavaReplies: 3Last Post: 04-23-2009, 06:16 AM -
terminating a while loop with a string
By tkdvipers in forum New To JavaReplies: 3Last Post: 07-09-2007, 11:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks