10 Replies - 4245 Views - Last Post: 18 March 2010 - 04:27 PM Rate Topic: -----

#1 Guest_DumbyGuy*


Reputation:

Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 12:29 PM

Code is not near to being done, but i need help on finding what I need to use for the program to recognize what the user inputs, in order to do either addition, subtraction, multiplication etc.. I know the sections after "if" and "else if" are wrong, but I do not know what to put there.
if you can provide any help for me, It would help a looooot. thanks :)

import javax.swing.JOptionPane;
import java.util.*;
public class Ifprogram
{
	static Scanner console = new Scanner(System.in);
	public static void main (String[] args)
		{
			String Intro;
			String Add;
			String Subtract;
			String Multi;
			String Divide;
			String Modu;
			String Exit;
			int A;
			int S;

			Intro = JOptionPane.showInputDialog(null, "Enter A to add 3 numbers \n S to Subtract two numbers \n M to Multiply three numbers \n D to Divide two numbers \n O to Modulus two numbers \n and E to Exit.");
			if (User inputs 'A') {
			Add = JOptionPane.showInputDialog(null, "You selected to add 3 numbers. Enter the first digit to add.");
			}	else if (User inputs 'S') {
			Subtract = JOptionPane.showInputDialog(null, "You selected to subtract 2 numbers. Enter the first digit to add.");
			}
		}
	}



Is This A Good Question/Topic? 0

Replies To: Help with Java Code - If Else Statements, recognizing user input

#2 aparajita15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-September 09

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 12:35 PM

include the buttons with add functionality added to it using actionevents.

and include 2 textboxes..in order to input two numbers, where user can write the no. of his choice.
Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 12:36 PM

In your if statement, just get the first letter of the input returned from the JOptionPane, and compare it to the various chars:
if(Intro.chatAt(0) == 'A'){
   // do some addtion
}


You could also use a switch statement, which would probably make your life easier.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_DumbyGuy*


Reputation:

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 12:50 PM

View Posterik.price, on 18 March 2010 - 11:36 AM, said:

In your if statement, just get the first letter of the input returned from the JOptionPane, and compare it to the various chars:
if(Intro.chatAt(0) == 'A'){
   // do some addtion
}


You could also use a switch statement, which would probably make your life easier.


Ok. Thanks.
But I am getting this compiling code.
It's not picking up the periods inbetween intro and chatAt

C:\*Ifprogram.java:20: cannot find symbol
symbol  : method chatAt(int)
location: class java.lang.String
			if(Intro.chatAt(0) == 'A') {
                                ^
C:\*Ifprogram.java:22: cannot find symbol
symbol  : method chatAt(int)
location: class java.lang.String
			}	 else if(Intro.chatAt(0) == 'S') {
                                              ^
2 errors

Tool completed with exit code 1

Was This Post Helpful? 0

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 12:51 PM

Sorry, typo. charAt(0);
Was This Post Helpful? 0
  • +
  • -

#6 Guest_DumbyGuy*


Reputation:

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 01:10 PM

View Posterik.price, on 18 March 2010 - 11:51 AM, said:

Sorry, typo. charAt(0);


awesome.. thank you.
Was This Post Helpful? 0

#7 Guest_DumbyGuy*


Reputation:

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 02:03 PM

Sorry to double post.. but I have another question.

How can i take inputs from the user, in a float format?
Was This Post Helpful? 0

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 02:05 PM

If you are using the JOptionPane approach, just use float result = Float.parseFloat(input); where input is the value returned from your JOptionPane

If you wanted to use a Scanner, there is a nextFloat method
Was This Post Helpful? 0
  • +
  • -

#9 Guest_DumbyGuy*


Reputation:

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 02:32 PM

Oh ok cool.

What would the Scanner method be for float?
Was This Post Helpful? 0

#10 Guest_DumbyGuy*


Reputation:

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 02:33 PM

View PostDumbyGuy, on 18 March 2010 - 01:32 PM, said:

Oh ok cool.

What would the Scanner method be for float?


oops, i mean nextfloat method.
Was This Post Helpful? 0

#11 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2706
  • View blog
  • Posts: 10,578
  • Joined: 15-July 08

Re: Help with Java Code - If Else Statements, recognizing user input

Posted 18 March 2010 - 04:27 PM

Yep...to take a float from Scanner, use nextFloat(). Actually, there are quite a few method in Scanner that you would find useful:

http://java.sun.com/...#method_summary
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1