Results 1 to 5 of 5
- 11-08-2009, 11:01 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
Do While with String varification
Hello I am in need of help. I have an assignment to do that needs to a do while loop to check if "m" or "s" has been typed in.
I know the line is incorrect and it is what I need help with. I need the while to continue until m or s is typed in but dont know how to do so! PLEASE HELP!Java Code:do { System.out.println("Enter offence type: "); offenceType = input.next(); if(offenceType.equalsIgnoreCase("m")) { offenceFee = minorValue; finalMinorOffences = finalMinorOffences + 1; } else if(offenceType.equalsIgnoreCase("s")) { offenceFee = SERIOUS_FINE; finalSeriousOffences = finalSeriousOffences + 1; } finalFee = finalFee + offenceFee; }while(offenceType != ("m" "s"));Last edited by Fubarable; 11-08-2009 at 11:10 PM. Reason: code tags added
-
One fix: use a boolean for your sentinel variable:
Java Code:boolean inputCorrect = false; do { System.out.println("Enter offence type (\"m\" / \"s\"): "); offenceType = input.next(); if (offenceType.equalsIgnoreCase("m")) { offenceFee = minorValue; finalMinorOffences = finalMinorOffences + 1; inputCorrect = true; // set inputCorrect variable here! } else if (offenceType.equalsIgnoreCase("s")) { offenceFee = SERIOUS_FINE; finalSeriousOffences = finalSeriousOffences + 1; inputCorrect = true; // set inputCorrect variable here! } finalFee = finalFee + offenceFee; //} while(offenceType != ("m" "s")); } while (!inputCorrect);
-
the other option is to use two calls to equalsIgnoreCase:
ps: don't forget to use code tags!Java Code:while (!offenceType.equalsIgnoreCase("m") && !offenceType.equalsIgnoreCase("s"));
pps: welcome to the forum!
- 11-08-2009, 11:24 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
Thank you very much! works like a charm :)
-
You're welcome. One more solution (get out your paper and pencil and start doing Venn diagrams!):
Java Code:while(!(offenceType.equalsIgnoreCase("m") || offenceType.equalsIgnoreCase("s")));
Similar Threads
-
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
Stuck on Boolean varification--plz help
By Cutter in forum New To JavaReplies: 7Last Post: 08-05-2009, 02:18 PM -
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks