Results 1 to 20 of 20
- 07-06-2011, 01:29 PM #1
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
Need help with exception handling for a string
I am writing a program that you have to enter either "R", "T", or "C" for some code to execute. I have everything running but cannot figure out how to handle if a different letter is entered or a number.....I want a reenter message to print and do again. I know how to use the numberFormat exception, but don't know how a string exception would be coded. Do I have to make my own? I tried using if statements, but that doesn't work. Heres the if's I had:
//get input from the user
System.out.println("Enter customer type (r/c/t): ");
System.out.println();
customerType = sc.next();
//check for non r/t/c entry
if(!customerType.equalsIgnoreCase("R")){
if(!customerType.equalsIgnoreCase("T"))
if(!customerType.equalsIgnoreCase("C"))
System.out.println("Re-enter");
}else{
System.out.println("Enter subtotal: ");
System.out.println();
double subtotal = sc.nextDouble();
this only works for R entry.
- 07-06-2011, 01:50 PM #2
Put the asking for the letter and the testing of the letter in a while loop.how to handle if a different letter is entered
Don't exit the loop unless you get a valid letter or the user asks to quit.
- 07-06-2011, 01:53 PM #3
Use a better statement?
Java Code:while(true) { if(!(myString.equalsIgnoreCase("R")||myString.equalsIgnoreCase("T")||myString.equalsIgnoreCase("C")) { //Code & way to exit loop } }- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-06-2011, 01:54 PM #4
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
But how do I while loop for 3 input letters r/c/t? Make 3 seperate while loops?
- 07-06-2011, 01:55 PM #5
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
I tried the OR if statement and couldn't get it to work.
- 07-06-2011, 01:57 PM #6
No, one is enough.
You need to work on your if test for valid input.
Your nested ifs are awkward.
- 07-06-2011, 01:58 PM #7
Please post your code with the errors or your explanation of what was the problem.I tried the OR if statement and couldn't get it to work.
- 07-06-2011, 02:01 PM #8
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
This was my if-OR's:
//check for non r/t/c entry
if(!customerType.equalsIgnoreCase("R")||!customerT ype.equalsIgnoreCase("T")||!customerType.equalsIgn oreCase("C")){
//if(!customerType.equalsIgnoreCase("T"))
// if(!customerType.equalsIgnoreCase("C"))
System.out.println("Re-enter");
}else{
{perform code}
It compiles and then running and entering r............
Enter customer type (r/c/t):
r
Re-enter
Enter customer type (r/c/t):
- 07-06-2011, 02:05 PM #9
The logic is wrong.Java Code:if(!customerType.equalsIgnoreCase("R")||!customerT ype.equalsIgnoreCase("T")
If customerType is "T" then it is NOT "R"
The first condition will be true
- 07-06-2011, 02:08 PM #10
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
But doesn't it look at all 3 conditions and if the input isn't r or t or c then
System.out.println("Re-enter");
?
- 07-06-2011, 02:13 PM #11
Write down the conditions and the boolean operators.But doesn't it look at all 3 conditions and if the input isn't r or t or c then
Set the variable to a value for testing
Then write down underneath each condition if it is true or false with that value of the variable.
Then apply the operators to those true and false values to get the final value for the expression.
With OR condition, the first true value will stop the rest of the compares.
With AND condition the first false value will stop the rest
- 07-06-2011, 02:14 PM #12
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
I got it to work from Dark's post....changing if(!customerType.equalsIgnoreCase("R")||!customerT ype.equalsIgnoreCase("T")||!customerType.equalsIgn oreCase("C")){
to... if(!(customerType.equalsIgnoreCase("R")||customerT ype.equalsIgnoreCase("T")||customerType.equalsIgno reCase("C"))){
worked!
thanks Dark
- 07-06-2011, 02:16 PM #13
Do you know why Dark's version works?
Writing boolean expressions takes a bit of study of the logic tables.
- 07-06-2011, 02:17 PM #14
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
not yet but am thinking bout it.
- 07-06-2011, 02:19 PM #15
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
something to do with the original was looking at !on each expression and was confused? and with !(expression) was not?
- 07-06-2011, 02:28 PM #16
Yes(regarding Norm's statement), don't just take the code. Make sure you understand it, otherwise you aren't going to become better at programming. The reason why mine works is because its looking for a true statement first. IE
With the input of R the check would stop at this statement customerType.equalsIgnoreCase("R"). Since it is true, the next part of the statement is looking to see if its not true.
!(true) = false;
If your condition of the if statement is false, then it won't enter the if statement.
Now if your input was T then it would get to the second or check. Ie: myString.equalsIgnoreCase("R")||myString.equalsIgnoreCase("T")
Since one of the conditions is met in your or statement, it is true. Resulting in another !(true) statement. This continues for as many conditions you have in your or statement.
Did that make sense?- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-06-2011, 02:30 PM #17
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
Yes that makes sense...thanks! Java can get a bit confusing sometimes.
- 07-06-2011, 02:36 PM #18
The hard part about any coding is the logic, and since you determine the logic used its on you how confusing it becomes. There are plenty of articles that can help explain how the logic of each syntax works, so don't be afraid to ask Google. I heard he is a pretty nice fellow. Most of the time you will find someone who had the same problem as you, and it will be explained right there for you so you don't have to wait. If you still have questions after that, then posting for further clarification is encouraged.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-06-2011, 02:42 PM #19
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
K, thanks, Dark
- 07-06-2011, 02:44 PM #20
No problem, if you have any more questions you need answered come back and visit. We don't bite, except for maybe Junky and Daryl, but they don't bite that often.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
Exception handling
By herat in forum New To JavaReplies: 1Last Post: 06-21-2011, 10:13 AM -
Exception Handling
By link6790 in forum New To JavaReplies: 16Last Post: 05-19-2011, 06:57 PM -
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 12:00 AM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks