• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with variables

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Walker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic