Brian Coppola - 6 months ago 42
Java Question

# Int cannot be dereferenced work around

I don't know if it is anywhere, but I was just wondering if there is any work around to the int cannot be dereferenced error. This is what I have so far

``````int ch;
JOptionPane.showMessageDialog (null, "1=+ 2=- 3=x 4=/ Please Choose Operation"); // Shows what numbers will do what operation when input
ch = Integer.parseInt (JOptionPane.showInputDialog("Please Choose Operation")); // Prompts user to input a number 1-4 for the operation they want to do
switch (ch) {
case 1:
JOptionPane.showMessageDialog(null,"You Chose Additon"); // Adds up all ten integers from above
JOptionPane.showMessageDialog(null, one + two + three + four + five + six + seven + eight + nine + ten);
break;
case 2:
JOptionPane.showMessageDialog(null,"You Chose Subtraction"); // Subtracts all 10 numbers, resulting answer can be negative
JOptionPane.showMessageDialog(null, one - two - three - four - five - six - seven - eight - nine - ten);
break;
case 3:
JOptionPane.showMessageDialog(null, "You Chose Multiplication Do not use zeroes");  // Multiplies all 10 integers, cannot use 0's since answer would be 0
JOptionPane.showMessageDialog(null, one * two * three * four * five * six * seven * eight * nine * ten);
break;
case 4:
JOptionPane.showMessageDialog(null,"You Chose Division Can't Divide by 0"); // Divdes all numbers, cannot use 0's because no dividing by 0
JOptionPane.showMessageDialog(null, one / two / three / four / five / six / seven / eight / nine / ten);
break;
default:
JOptionPane.showMessageDialog(null,"You have to choose an operation"); // If no operation is chosen, Program ceases and closes
break;
``````

And I want to know if there is any way i can make it force you to input an operation. I tried doing

``````    boolean validInput2 = false;
while (!validInput){
if (ch.endswith(" "));
``````

but that doesn't seen to work (I know that's not the whole code, I just keep getting stuck at the dereference error) is there anything I can do to work around this error and still have the code working?

it worked for me with some minor fixes.

``````int ch;
JOptionPane.showMessageDialog (null, "1=+ 2=- 3=x 4=/ Please Choose Operation"); // Shows what numbers will do what operation when input
ch = (int)JOptionPane.showInputDialog("Please Choose Operation").charAt(0); // Prompts user to input a number 1-4 for the operation they want to do
switch (ch) {
case 1:
JOptionPane.showMessageDialog(null,"You Chose Additon"); // Adds up all ten integers from above
JOptionPane.showMessageDialog(null, one + two + three + four + five + six + seven + eight + nine + ten);
break;
case 45:
JOptionPane.showMessageDialog(null,"You Chose Subtraction"); // Subtracts all 10 numbers, resulting answer can be negative
JOptionPane.showMessageDialog(null, one - two - three - four - five - six - seven - eight - nine - ten);
break;
case 3:
JOptionPane.showMessageDialog(null, "You Chose Multiplication Do not use zeroes");  // Multiplies all 10 integers, cannot use 0's since answer would be 0
JOptionPane.showMessageDialog(null, one * two * three * four * five * six * seven * eight * nine * ten);
break;
case 4:
JOptionPane.showMessageDialog(null,"You Chose Division Can't Divide by 0"); // Divdes all numbers, cannot use 0's because no dividing by 0
JOptionPane.showMessageDialog(null, one / two / three / four / five / six / seven / eight / nine / ten);
break;
default:
JOptionPane.showMessageDialog(null,"You have to choose an operation"); // If no operation is chosen, Program ceases and closes
break;
}
``````

if you want to switch with the int you will need to switch over the ascii-codes of the characters, just like i did for '-' character. you can also switch by the characters itself like this :

`````` char ch = JOptionPane.showInputDialog("Please Choose Operation").charAt(0);
``````

and then switch like this

``````switch (ch) {
case '+':
``````

with this you have to type the character + - * / in order for the program to work. i hope its understandable what i want to tell you and i hope its what you are looking for.

you can force the user to input a char like this :

``````char ch = 0;
JOptionPane.showMessageDialog (null, "1=+ 2=- 3=x 4=/ Please Choose Operation");
while(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'){
ch = JOptionPane.showInputDialog("Please Choose Operation").charAt(0);
}
``````

-->switch-statement goes after while-loop

Source (Stackoverflow)