3 Replies - 1857 Views - Last Post: 11 February 2012 - 02:21 PM Rate Topic: -----

#1 CarlosAAlvarez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Scanner as input and JOptionPane as output.

Posted 10 February 2012 - 10:48 PM

Hello everyone, I have been stuck trying to figure out how to output if an integer is a multiple of another integer. I know how to output the information if i was to use Scanner as my output, but the task is to use Scanner as my input and JOptionPane as my output. If someone here could show me in the correct direction I would be very thankful. Now I understand that in my code I have JOptionPane.showMessageDialog at the very end and thats why in the "message box" I always output "int is a multiple of another int" but no matter what I enter and integers I get the same output even if the integers are not multiples of each other. So basically I want it to be accurate when entering the information. Here is my code:


 


import java.util.Scanner;
import javax.swing.JOptionPane;

public class Multiples 
{
	public static void main (String args [] )
	{
		Scanner input = new Scanner (System.in); 

		
		int number1;
		int number2;
		
		String message = "";



			System.out.println( "Enter first integer" );
			number1 = input.nextInt();

			System.out.println( "Enter second integer" );
			number2 = input.nextInt();



			if (number1 % number2 != 0);
				message = number1 + " is not a multiple of " + number2;


		
			if (number1 % number2 == 0);
				message = number1 + " is a multiple of " + number2;

			
			JOptionPane.showMessageDialog(null, message);

 
	}

			


} 



Is This A Good Question/Topic? 0
  • +

Replies To: Scanner as input and JOptionPane as output.

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Scanner as input and JOptionPane as output.

Posted 10 February 2012 - 11:14 PM

You have a semicolon after your if statements, and since you're not using brackets, the semi-colon (or blank statement), eats your if statements. The code you think is being executed only if a condition is true is being executed regardless. So you will always get the last one there.

TL:DR Remove the ; after your if statements.
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Scanner as input and JOptionPane as output.

Posted 10 February 2012 - 11:17 PM

The problems is the semicolon infront of the if conditions:
if (number1 % number2 != 0);

Remove those semicolons, also you can use if else instead...
Was This Post Helpful? 1
  • +
  • -

#4 CarlosAAlvarez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: Scanner as input and JOptionPane as output.

Posted 11 February 2012 - 02:21 PM

Thank you Sheph and smohd, wow such an easy fix and I dint see that, learning as I go, thanks alot. both post were helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1