This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need help with variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need help with variables" Watch "Need help with variables" New topic
Author

Need help with variables

John Walker
Greenhorn

Joined: Oct 28, 2003
Posts: 9
Hi all, I'm trying to create a program where the user inputs two numbers, the first will be the multiplication table and the second will be by how many many times tables to be shown. I'm having trouble getting the variables right.
import javax.swing.JOptionPane;

public class InputMultiply {

public static void main (String[]args) {
String input = JOptionPane.showInputDialog("Enter a multiplier");

String inputa = JOptionPane.showInputDialog("Enter the number of problems you want to display");

int input1 = input;
int input2 = inputa;
int answer;

for (input=1; input2 <= input2; input2++)
{
answer = input1 * input2;
System.out.println(input1 + "*" + input2 + "=" + answer);
}



System.exit(0);
}
}

I get the error:
C:\java\bin>javac InputMultiply.java
InputMultiply.java:13: incompatible types
found : java.lang.String
required: int
int input1 = input;
^
InputMultiply.java:14: incompatible types
found : java.lang.String
required: int
int input2 = inputa;
^
InputMultiply.java:18: incompatible types
found : int
required: java.lang.String
for (input=1; input2 <= input2; input2++)

Any suggestions?
Jerry Callo
Greenhorn

Joined: Nov 04, 2003
Posts: 4
When you get the value from the input box it's a string. you have to convert it to an integer instead of assigning it directly to an int variable. Try input1 = Integer.parseInt(input)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

First, a general comment: I see variables named input1, input2, intput, and inputa in this short program. You're going to drive yourself insane that way. Use "multiplierString", "displayCountString", "multiplier," and "displayCount". See how much more sense that would make?
Second, ints and Strings are different things and not trivially interconvertable. You can't assign a String value to an int variable, or vice-versa.

Third, one specific piece of information: you can use the static method Integer.parseInt() to turn a String into an int. Be sure to catch NumberFormatException!
Fourth, in your "for" loop, use a new variable for the counter (people often use "i" for a counter name). By reusing the existing variables, you've created something so confused that I can't even tell what it is you're trying to do.


[Jess in Action][AskingGoodQuestions]
John Walker
Greenhorn

Joined: Oct 28, 2003
Posts: 9
Thanks for the help. For the loop I just want it to display the multiplier (let's say 2) and the second input # will be the number of times tables displayed (let's say 4). So the loop will go around and outupt 2x1=2, 2x2=4, 2x3=6, 2x4=8 and end.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help with variables
 
Similar Threads
Help on testprogram and subclass please
cant find method from JOptionPane class
Problem with Generics Wildcard
JOption problems
product all array elements without the first element