Brian Coppola
Brian Coppola

Reputation: 25

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?

Upvotes: 1

Views: 133

Answers (2)

XtremeBaumer
XtremeBaumer

Reputation: 6435

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

Upvotes: 3

Aimee Borda
Aimee Borda

Reputation: 842

Another option is to create a mapping from the operation to the message and then the result will be immediate from the map.

    HashMap<Integer, String> map = new HashMap<>();
    map.put(43, "You chose Addition" + (1+2+4));
    map.put(45, "You chose Subtraction" + (1-2-4));
    map.put(47, "You chose Division" + (1/2));
    map.put(42, "You chose Multiplication" + (1*2*4));

    String operations  =Arrays.toString(map.keySet().stream().map(k -> (char)(int)k).toArray());
    int val = JOptionPane.showInputDialog(null,"enter operation: "+ operations).charAt(0);
    JOptionPane.showMessageDialog(null, map.get(val));

Update To avoid computing the result aprior, we can make use of Callable:

    HashMap<Integer, Callable<String>> map = new HashMap<>();
    map.put(43, () -> {
        System.out.println("hello");
        return "You choose Addition" + (1+2+3);
    });
    map.put(45, () -> "You chose Subtraction" + (1-2-4));
    map.put(47, ()->"You chose Division" + (1/2));
    map.put(42, () -> "You chose Multiplication" + (1*2*4));

    String operations  =Arrays.toString(map.keySet().stream().map(k -> (char)(int)k).toArray());
    int val = JOptionPane.showInputDialog(null,"enter operation: "+ operations).charAt(0);
    JOptionPane.showMessageDialog(null, map.get(val).call());

Upvotes: 0

Related Questions