Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have the request for the numbers on the JoptionPane and the Command line but I dont know how to store them to multiply them together, can you please have a look and suggest any options I can use to get it working, here is my current code :



C#
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
 *
 * @author Adam
 */
public class Inputting {


    public static void main(String[] args) {
        Scanner Scan = new Scanner (System.in); // Creates the Scanner to allow input



        System.out.print("Enter a Number");
        String input = null;
        String first = Scan.next();



        String Second; // Second Declared as a string
        Second = JOptionPane.showInputDialog("Enter Another Number");
        double d = Double.parseDouble(Second);

        System.out.print("What is your Name?");
        String name = Scan.next();

        String Age; // Age Declared as a String
        Age = JOptionPane.showInputDialog("Enter your age ");

        System.out.print( name + " "+ "(Aged" + " " + Age +  ")" + "," + "your answer is " + first*Second );








    }

}
Posted
Comments
Sergey Alexandrovich Kryukov 21-May-15 12:46pm    
What's your problem?
—SA
Adamdew93 21-May-15 13:09pm    
The program seems to have an issue with the '*' as the error "bad operand types for binary operator " appears and I am struggling to get rid of it

1 solution

Ok really basic:

You got your input values as String.

Java
...
String first = Scan.next();
...
Second = JOptionPane.showInputDialog("Enter Another Number");


After this you convert only
Java
second
into a numeric value, but don't use it!

Java
double d = Double.parseDouble(Second);


Solution is to convert
Java
first
and
Java
Second
into numeric values like this:

Java
double firstValue = Double.parseDouble(first);
double secondValue = Double.parseDouble(Second);

// get the result
double result = firstValue * secondValue;


Now you can print it:

Java
System.out.print( name + " "+ "(Aged" + " " + Age +  ")" + "," + "your answer is " + result );
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900