3 Replies - 719 Views - Last Post: 02 December 2009 - 05:15 PM Rate Topic: -----

#1 mike_clarke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-December 09

My first GUI app problem

Post icon  Posted 02 December 2009 - 04:36 PM

Hey everyone, this is my first (of many) posts here on Dreamincode :)
I started learning java last week, getting realy into it and have moved from console apps to my very first GUI app. It is something i made myself, which does the job i wanted it to do but with 1 small problem. I have 3 if statements and then an else statement, even when one of the if statments is met, it still runs the else statement. Heres the code:

import javax.swing.JOptionPane;

class apples{
	public static void main(String[] args){
		String fn = JOptionPane.showInputDialog("First Number");
		String sn = JOptionPane.showInputDialog("Second Number");
		
		int num1 = Integer.parseInt(fn);
		int num2 = Integer.parseInt(sn);
		
		String wtf = JOptionPane.showInputDialog("1='x' 2='+' 3='-'");
		
		int opwtf = Integer.parseInt(wtf);
		int ans = 0;
		
		if(opwtf == 1){
			ans = num1 * num2;
		}
		if(opwtf == 2){
			ans = num1 + num2;
		}
		if(opwtf == 3){
			ans = num1 - num2;
		}
		else{
			JOptionPane.showMessageDialog(null, "I dont understand", "ERROR MSG", JOptionPane.PLAIN_MESSAGE);
			
		}
		JOptionPane.showMessageDialog(null, "The Answer is: " + ans, "Answer", JOptionPane.PLAIN_MESSAGE);
		
		
		
	}


Its ment to take 2 numbers from the user, then a 3rd number to state what to do with the 2 numbers (1 for x, 2 for + and 3 for -), give an error if 1,2 or 3 not entered, if all good, displays the answer WITHOUT the error message.

Thanks for any help :)

This post has been edited by mike_clarke: 02 December 2009 - 04:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My first GUI app problem

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: My first GUI app problem

Posted 02 December 2009 - 04:49 PM

you should use the else if, instead of only if statements.
i updated the problrmatic code:
if(opwtf == 1){
			ans = num1 * num2;
		}
		else if(opwtf == 2){
			ans = num1 + num2;
		}
		else if(opwtf == 3){
			ans = num1 - num2;
		}
		else{
			JOptionPane.showMessageDialog(null, "I dont understand", "ERROR MSG", JOptionPane.PLAIN_MESSAGE);
			
		}



when using more than one if statement you should use else if. (in case there a logical connection between the if)

when you use the if, else if statements you actually tell the compiler that:
if condition1, do something.
else if not condition1, do something else.

when you dont use the if, else if, the compiler understand it as:
if condition1 do something.
if condition2 also, do something else too.

see the difference?
Was This Post Helpful? 1
  • +
  • -

#3 mike_clarke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-December 09

Re: My first GUI app problem

Posted 02 December 2009 - 04:53 PM

ah i get it, so that would explain why it didnt display the error message when the last if statement was true. Thanks for the help japanir :)
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: My first GUI app problem

Posted 02 December 2009 - 05:15 PM

View Postmike_clarke, on 2 Dec, 2009 - 03:53 PM, said:

ah i get it, so that would explain why it didnt display the error message when the last if statement was true. Thanks for the help japanir :)

Yes your else applied only to that if

if(opwtf == 3)
...
else
optionPane()

"My first GUI app problem " bet wont be your last one :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1