Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-15-2003, 05:13 PM   #1
Registered: Oct 2003
Posts: 131

Rep: Reputation: 15
quick question - java

In java, jdk2.4.1 i am trying to let a user choose options, a small program that is supposed to be a calculator. with 4 options with the output being:



Can somebody tell me how I can allow a user to choose option 1 or 2 and let the computer do the calculation (if use chooses one, how can i let him choose 2 integers to add)

Old 10-15-2003, 08:56 PM   #2
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
do {

  // either set limit to number of variables (like only 2), or store them all in a vector (using some delimter- like 2+2+2+2 ..... with the operation symbols as delimiters)

  switch (user_selection) {
      case 1: add_vars; break;
      case 2: sub_vars; break;
      default: wtf; break;
} while (not_quit)

so you basically have to just read the options the user enters, and just print out the result of the operation. dont know if i was of any help here ... maybe i shoulda written java code ...
Old 10-15-2003, 09:56 PM   #3
Senior Member
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
if this is a console app, you can read input from the console using this:

InputStreamReader ins = new InputStreamReader (;
BufferedReader input = new BufferedReader (ins);
String strInput = "";
int i, j;

//this will get the input
	strInput = input.readLine();

	//check for valid operand here

	//to get input for integers using strInput = input.readLine and parsing
	//an integer using Integer.parseInt (strInput);
		//get integers here using Integer.parseInt
	catch (Exception e)
		System.out.println ("ParseInt error. You didn't enter an integer");
catch (IOException e)
	e.printStackTrace ();
from here you can reuse strInput for getting the operation, the first integer, and the second integer. you can use Integer.parseInt (strInput); to try to parse an integer.

Last edited by megaspaz; 10-15-2003 at 09:58 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM
Quick Java question Nylex Programming 2 01-26-2004 12:44 PM
Quick Java plug-in question Tyir Linux - Newbie 4 10-09-2003 01:58 PM
Quick question: Does Java 1.4.2 unstable in your PC? codec Linux - Software 3 09-16-2003 11:24 AM
2 Quick Java Questions Crashed_Again Programming 13 09-10-2003 04:57 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration