Computing.Net > Forums > Programming > Java decision

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java decision

Reply to Message Icon

Name: MiKeY
Date: September 12, 2004 at 12:09:17 Pacific
OS: WinXP Home
CPU/Ram: 2.4GHz Althlon XP/768MB
Comment:

How can I adapt this program so that the user would be given another go irrespective of whether or not an upper case or lower case 'y' is entered?

I have read the 'Taking Control' chapter countless times, and I'm no closer to a solution. I'm sure It's something simple though.

/* program prompts user for two numbers, adds them together, and runs
until user chooses to quit */

public class Chapter3Arithmetic
{
public static void main(String[] args)
{
int num1, num2;
char choice;
do
{
System.out.print("Enter a number: ");
num1 = EasyIn.getInt();
System.out.print("Enter another number: ");
num2 = EasyIn.getInt();
System.out.println("The sum of the two numbers is "
+ (num1 + num2));
System.out.print("Do you want another go (y/n): ");
choice = EasyIn.getChar();
}
while(choice == 'y');
}
}



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: September 12, 2004 at 12:29:38 Pacific
Reply:

Don't do JAVA.

Either convert choice to lower case and test 'y' or test for both

Very good, thinking about user input possibilities.


0

Response Number 2
Name: MiKeY
Date: September 12, 2004 at 14:48:18 Pacific
Reply:

thanks. that's exactly what i can't figure out? how can i test for both conditions, as well as other possible errors such as numerical input? which construct will work best?


0

Response Number 3
Name: BlueRaja
Date: September 12, 2004 at 15:32:13 Pacific
Reply:

switch (choice.toLowerCase())
{
case 'y':
//yes
break;

case 'n':
//no
break;

default:
//Invalid input
break;
}

Dunno if java uses ?: , but if it does, this could also be written as
(choice.toLowerCase() == 'y')?(/*yes*/):(/*any other input*/)

AKhalifman@hotmail.com


0

Response Number 4
Name: BlueRaja
Date: September 12, 2004 at 15:33:58 Pacific
Reply:

Oh, sorry, didn't read the question; if you want to keep the code the way it is, simply use the toLowerCase() String method.

AKhalifman@hotmail.com


0

Response Number 5
Name: MiKeY
Date: September 12, 2004 at 15:47:14 Pacific
Reply:

i'm a java newebie (like you didn't guess). i'm sure there's a simple solution to the problem that doesn't involve using a pre-defined java class?

can this be achieved using an if, if...else, or switch statement, for example?

i'm working my through 'JAVA in two semesters', but the solutions are only available to lecturers. hmmm :/ v frustrating.


0

Related Posts

See More



Response Number 6
Name: BlueRaja
Date: September 12, 2004 at 17:44:27 Pacific
Reply:

bah. I just noticed that you used a char, not a String.
I just started Java (although I've been programming in C++ for a while), but I don't think there's a way to directly edit chars at a low level (as there is in C++).
In C++, you'd use the tolower() function; I don't know if there is an equivalent method in Java.

AKhalifman@hotmail.com


0

Response Number 7
Name: Don Arnett
Date: September 12, 2004 at 18:18:46 Pacific
Reply:

while(choice == 'y' || choice == 'Y');

I don't do Java much, but in C/C++ OR is ||. I believe that it is also in Java, but it could be the word 'or', as in:

while (choice == 'y' or choice == 'Y');


I wouldn't worry about numeric input, because any input will be accepted as a character. So if they type '1', it will be the same as typing 'n', 'q', 'x', etc. The loop test will fail, thus the loop will stop.



0

Response Number 8
Name: MiKeY
Date: September 13, 2004 at 12:37:02 Pacific
Reply:

thanks guys


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java decision

java programming www.computing.net/answers/programming/java-programming/14688.html

Programming in java www.computing.net/answers/programming/programming-in-java/7510.html

Java SDK www.computing.net/answers/programming/java-sdk/8718.html