Java

posted by .

I am supposed to convert a program i already made into a Java console application that uses the Scanner class to read the user's input. This is the code i have that i need to convert. I don't know where to start. Here is the code

import javax.swing.JOptionPane;
public class HW3 {





public static void main (String args[])
{

String firstNum;
String secondNum;
int num1;
int num2;
int sum;
int difference;
int quotient;
int product;


firstNum = JOptionPane.showInputDialog("Enter first integer: " );
secondNum = JOptionPane.showInputDialog("Enter second integer: " );
num1 = Integer.parseInt( firstNum );
num2 = Integer.parseInt( secondNum );
sum = num1 + num2;
difference = num1 - num2;
product = num1 * num2;
quotient = num1 / num2;


JOptionPane.showMessageDialog(null, "The sum is " + sum,
"Results", JOptionPane.PLAIN_MESSAGE );
JOptionPane.showMessageDialog(null, "The difference is " + difference);
JOptionPane.showMessageDialog(null, " The quotient is" + quotient);
JOptionPane.showMessageDialog(null, "The product is" + product);
System.exit( 0 );
}
}

  • Java -

    The Scanner method for reading in an int is nextInt().

    You need to instantiate a Scanner, and replace the inputs and outputs.

    Instead of

    firstNum = JOptionPane.showInputDialog("Enter first integer: " );
    secondNum = JOptionPane.showInputDialog("Enter second integer: " );

    using a Scanner, you will have

    // instantiate scanner
    Scanner kb = new Scanner(System.in);
    // output dialog
    out.print("Enter first integer: ");
    // input int
    firstNum = kb.nextInt();
    // second int
    out.print("Enter second integer: ");
    secondNum = kb.nextInt();

    Now you need to change your output.

    Instead of

    JOptionPane.showMessageDialog(null, "The sum is " + sum,

    include simple printlines.

    System.out.println("The sum is "+sum);

    And do the same for the rest of the output.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Java help please

    I am trying to run the following program and am getting this error: java.util.InputMismatchException import java.io.*; import java.util.*; public class Pre2 { static Scanner console = new Scanner(System.in); public static void main(String[] …
  2. programming

    write a program that maintains two parallel arrays. after th arrays have been loaded the program should behave as a login screen prompting for username and password. the program should respond three output messages. if username does …
  3. java

    can anyone help me with this? I'm new to java! Write a program that will find the lowest common denominator of two numbers. The user will enter the two denominators, the program will output both the lowest common denominator and the
  4. java

    Write a program that will find the lowest common denominator of two numbers. The user will enter the two denominators, the program will output both the lowest common denominator and the multipliers required for each fraction. For example, …
  5. java

    Write a java application that finds the smallest of several integers. Prompt the user for the number of integers that will be input, then prompt the user for that number of integers. Evaluate the integers to determine the smallest …
  6. Java Programming

    I need to create a pseudocode for the follwing application: Write Java-like pseudocode to specify the logic for the following program application: Write an application that prompts an employee for an hourly pay rate and hours worked. …
  7. programming

    Write a program which allows user to enter 10 student names and scores for Java programming 1 and Java programming 2. Allow user to choose which course they want to enter the record for. Use the loop to ask input from user. The program …
  8. java programming

    the learner is asked to write a program using java with user input that caculate and displays, the number of months it will take the user to save an amount of money. the user must enter the
  9. pseudo code algorithm

    Convert your algorithm from Question One into a complete Java application. Marks will be allocated based upon: ● Your Java code matching your pseudo code algorithm from Question One. ● Your Java code being syntactically …
  10. Java programming

    Okay so I am rewrite the program and it's still not coming right can anyone help me see what's going wrong here please I have to turn this in by tmw so here the program The following program is supposed to read two numbers from a file …

More Similar Questions