Results 1 to 20 of 22
- 06-08-2009, 04:18 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
[SOLVED] how to run loop unless and until user enter correct value
Hello everyone,
I'm trying write such which ask user again to enter correct value if user enter wrong one.
like I'm using this code..
import java.io.*;
import java.util.*;
public class test {
public static void main(String[] args) {
boolean t = true;
String g;
int age;
Scanner s = new Scanner(System.in);
do{
System.out.println("Enter gender: m/f");
g = s.next();
System.out.println("Enter age");
age = s.nextInt();
System.out.println("Continue....true/false");
t=s.nextBoolean();
}while(t);
}
}
if user instead of enter m or f or male or female he enter something else like
'k' or anything else the loop goes back and ask user to enter correct value
how can I do this...
please any suggestion
- 06-08-2009, 05:15 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
I assume you now can, using this example, do the loop for the age aswell (e.g. negative or absurd low/high age)Java Code:do{ do{ System.out.println("Enter gender: m/f"); g = s.next(); while (!g.equalsIgnoreCase("m") && !g.equalsIgnoreCase("f")); System.out.println("Enter age"); age = s.nextInt(); System.out.println("Continue....true/false"); t=s.nextBoolean(); }while(t);I die a little on the inside...
Every time I get shot.
- 06-08-2009, 05:18 PM #3
- 06-08-2009, 06:32 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
Hi Supamagier,
This code is not working, I've change && to || but nothing happening.
Hi Ramyasivakanth,
Thanks for such informative notes but I didn't any suitable information for me.
please help me out in this I've completed almost eveything just stuck in this section.
- 06-08-2009, 06:35 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
Hi Supamagier,
This code is not working, I've change && to || but nothing happening.
Hi Ramyasivakanth,
Thanks for such informative notes but I didn't any suitable information for me but they are really informative thanks again for such amazing notes.
please help me out in this I've completed almost eveything just stuck in this section.
- 06-08-2009, 08:40 PM #6
Please explain
mfaizan24: just stating that the code isn't working, doesn't help in understanding the problem you're having. Please post your code (with Supamagier's suggested changes) and explain what the problem you're having with this code.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 06-08-2009, 09:20 PM #7
using a helper method would make this more clear.
Java Code:// pseudo code: ask for input while input != m or f print error, ask for input. repeat this loop. return input
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 06-09-2009, 04:17 AM #8
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
import java.io.*;
import java.util.*;
public class test {
public static void main(String[] args)throws Exception {
boolean t = true;
String g="";
int age;
Scanner s = new Scanner(System.in);
do{
System.out.println("Enter gender: m/f");
g = s.next();
while (!g.equalsIgnoreCase("m") || !g.equalsIgnoreCase("f"));
System.out.println("Enter age");
age = s.nextInt();
System.out.println("Continue....true/false");
t=s.nextBoolean();
}while(t);
}
}
here's my complete codes
- 06-09-2009, 07:04 AM #9
does it do what you wanted?
also this is shorter:
Java Code:while (!g.matches("^[mMfF]$")) continue;USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 06-09-2009, 12:56 PM #10
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
No it doesn't do what I want, All I want this loop should continue unless and until user enter the correct value
eg.
Enter gender m/f
k
// if user here press something like k or l instead of m/f it should go back and ask user again to enter correct value.
but above posted codes are working but screen goes stuck.
- 06-09-2009, 01:12 PM #11
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
Hi,
I've change while to if statement and its working perfectly fine
public static void main(String[] args) {
String g;
int age;
boolean t=true;
Scanner sc = new Scanner(System.in);
do{
System.out.println("enter gender");
g = sc.next();
if(!g.matches("^[mMfF]$")){
continue;
}
System.out.println("Enter age");
age = sc.nextInt();
if(age < 0){
continue;
}
System.out.println("continue!!....true/false");
t = sc.nextBoolean();
}while(t);
but stuck in age
}
- 06-09-2009, 02:38 PM #12
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
You shouldn't use code you don't understand (I don't think you do tbh)Java Code:if(!g.matches("^[mMfF]$"))I die a little on the inside...
Every time I get shot.
- 06-09-2009, 03:19 PM #13
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
How can I apply this codes for age..
System.out.println("Enter age");
age = sc.nextInt();
if(age < 0){
continue;
}
it doesn't work..
- 06-09-2009, 05:51 PM #14
what's not working?
Java Code:S:\>java Test enter gender m Enter age 0 continue!!....true/false false END S:\>java Test enter gender m Enter age -2 enter gender m Enter age 1000 continue!!....true/false false END
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 06-09-2009, 06:00 PM #15
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
m
Enter age
-2 //Here I'm stuck
enter gender
m
Enter age
1000
continue!!....true/false
false
END
=============
It shouldn't go back to gender it should stay at age only
like if user enter 0 or -2
it should ask user to enter age
not enter gender
but this loop is going back to gender which is already checked.
Please help me out...
- 06-09-2009, 06:24 PM #16
that cause you changed while to an if
You want:Java Code:while(!g.matches("^[mMfF]$")){ [B] g = sc.next(); // Dop! update user input[/B] //continue; }
While Not M or F, loop
While Age < 0, loop
If True, Then continue
Else End.Last edited by angryboy; 06-09-2009 at 06:27 PM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 06-09-2009, 09:38 PM #17
hhhmmm...
A couple of comments/suggestions...
Maybe two "while" loops are needed for this cases (I'm pretty sure that one can be used, but it would probably confuse the OP)
And then do the same (another loop) for the age verification.Java Code:[LIST][*]genderOK = false [*]start while loop (!genderOK)[*]ask for gender[*]compare if gender is correct[*]if yes, genderOK = true[*]end while loop[/LIST]
Now to do it all in one loop...
.... or something close to that (check the "||" logic... it might be "&&").Java Code:[LIST][*]genderOK = false[*]ageOK = false[*]start while loop (!genderOK && !ageOK) [*]if genderOK == false[*]{[*]ask for gender[*]compare if gender is correct[*]if yes, genderOK = true[*]}[*]ask for age[*]compare if age is correct[*]if yes, ageOK = true[*]end while loop[/LIST]
Luck,
CJSLLast edited by CJSLMAN; 06-10-2009 at 01:49 AM. Reason: Corrected the logic
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 06-09-2009, 10:21 PM #18
for the one loop pseudo code, put in a "continue;" inside the if statement. and use &&.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 06-10-2009, 04:54 PM #19
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
I am sorry I really didn't understand anything..
- 06-10-2009, 05:08 PM #20
OK, in that case...
Here are some links you have to study to understand:
- The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
- The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
- The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
- Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
- Java: Boolean
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
what code for not hitting enter?
By tornbacchus in forum New To JavaReplies: 4Last Post: 04-11-2009, 04:59 AM -
enter key problem
By masa in forum AWT / SwingReplies: 3Last Post: 03-19-2009, 07:30 AM -
[SOLVED] User Input - loop
By new person in forum New To JavaReplies: 4Last Post: 02-22-2009, 10:02 PM -
loop when there is no user-input
By becky in forum New To JavaReplies: 12Last Post: 02-02-2009, 10:02 PM -
How do check I allow user to enter only alpha numeric and -?
By tanalyw in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks