简体   繁体   中英

error: incompatible types: String cannot be converted to int

I'm very new to programming and very very new to Java, so I'm sure this question (and all my other mistakes) will be very obvious to anyone with experience. I clearly don't know what I'm doing. My assignment was to convert Python code to Java. Below is my code.

My two error codes are:

Assignment1.java:37: error: incompatible types: String cannot be
converted to int 
response = reader.nextLine();
Assignment1.java:38: error: incompatible types: int cannot be converted to String 
num = Integer.parseInt(response);

For clarification, I'm using JGrasp, but willing to not.

import java.util.Scanner;
public class Assignment1 {
      public static void main(String[] args) {
    
         int num, response, min, max, range, sum, count, avg;
         Scanner reader = new Scanner(System.in);
         String reportTitle;

    
         //title
         System.out.println();
         System.out.println("Enter a title for this report");
         reportTitle = reader.nextLine();
         System.out.println();
         
         
         //data input
         System.out.println("Enter a series of numebr, ending with a 0");
         
         num = 0;
         sum = 0;
         response = 1;
         count = 0;
         max = 0;
         min = 0;
         range = 0;
         
         while (true)
         {
            if (response < 1)
            {
               System.out.println("End of Data");
               break;
            }
            
          System.out.println("Enter number => ");
          response = reader.nextLine();
          num = Integer.parseInt(response);
          
          sum += num;
          count += 1;
          
          if (num>max)
          {
            max = num;
          }
          if (num<min)
          {
            min = num;
          }
          
         }
         
         //crunch
         avg = sum / count;
         range = max - min;
         
         //report
         System.out.println();
         System.out.println(reportTitle);
         System.out.println();
         System.out.println("Sum: => "+sum);
         System.out.println("Average: => "+avg);
         System.out.println("Smallest: => "+min);
         System.out.println("Largest: => "+max);
         System.out.println("Range: => "+range);

    
    }

}

response is an int and the method nextLine() of Scanner returns... well, a line represented by a String . You cannot assign a String to an int - how would you do so? A String can be anything from "some text" to " a 5 0 " . How would you convert the first and/or the second one to an int ?

What you probably wanted is:

System.out.println("Enter number => ");
response = reader.nextInt();
// notice this        ^^^

instead of:

System.out.println("Enter number => ");
response = reader.nextLine();
// instead of this    ^^^^

the nextInt() method expects a sequence of digits and/or characters that can represent a number, for example: 123 , -50 , 0 and so on...

However, you have to be aware of one significant issue - mixing nextLine() and nextInt() can lead to undesirable results. For example:

Scanner sc = new Scanner(System.in);
String s;
int a = sc.nextInt();
s = sc.nextLine();
System.out.println(s);

Will print nothing , because when inputting the number, you type for example 123 and hit enter , you additionally input a newline character. Then, nextLine() will fire and will scan every character up to the first enountered newline or end-of-file character which apparently is still there , unconsumed by nextInt() . To fix this and read more about it go here

To read an integer from a file you can simply use an example like this:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

However, you dont give further information about your file structure so my advice is to look again in the documentation to find what best fits you. You can find the documentation of scanner here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM