Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

int cannot be dereferenced and cant read if else statement condition

I am new in Java im having a hardtime solving the problem in the "response = first.nextint(); and response = second.nextint();" and if i removed response first and second the code works but cant read the conditions if else when inputing a random number and a Indicated letters inside the if else.

import java.util.Scanner;

public class scanMe 

{

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    public static void main (String[] args)
    {
         int first = '.';
        int second = '.';
        String response = ".";
        
        Scanner scanMe = new Scanner(System.in);
         System.out.println("Please input first Number: ");
        
  response = first.nextint();
  

    System.out.println("Please input second Number: ");
   
     response = second.nextint();
     
 System.out.println("Please Press A for Addition");
 System.out.println("Please Press B for Subtraction");
 System.out.println("Please Press C for Multiplication");
 System.out.println("Please Press D for Division");
 System.out.print("Input any letter: ");
    response = scanMe.nextLine();

if(response ==("A")){

    System.out.println("The Result is first+second");
    
                 }
                    
else if(response ==("B")){
    
    
    System.out.println("The Result is first+second");
    
                      }
 else if(response ==("C")){
  
    System.out.println("The Result is first+second");
    
                        }
  else if(response ==("D")){
   
    System.out.println("The Result is first+second");
                            }
                            
   else  
         System.out.println("Letter cannot be identify and is not registered to any function");
    }

}



>Solution :

Your response is declared as string and the Scanner.nextInt() method returns an int. Change the type of response to int:

int response = 0;

Another issue with your code, you should invoke scanMe.nextInt(), not intvaraible.nextInt()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading