LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-15-2003, 05:13 PM   #1
Laptop2250
Member
 
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:

Calculator

1.Addition
2.Subtration

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)

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

Rep: Reputation: 45
Code:
do {
  print_out_user_options;

  // 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)
  read_user_selection; 

  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
megaspaz
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:

Code:
InputStreamReader ins = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (ins);
String strInput = "";
int i, j;

//this will get the input
try
{
	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);
	try
	{
		//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.
 
  


Reply


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


All times are GMT -5. The time now is 04:03 PM.

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