简体   繁体   English

错误:不兼容的类型:字符串无法转换为 int

[英]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.我对编程很陌生,对 Java 也很陌生,所以我相信这个问题(以及我所有的其他错误)对于任何有经验的人来说都是非常明显的。 I clearly don't know what I'm doing.我显然不知道我在做什么。 My assignment was to convert Python code to Java.我的任务是将 Python 代码转换为 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.为了澄清起见,我正在使用 JGrasp,但我不愿意。

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 . response是一个int并且Scanner的方法nextLine()返回......好吧,一个由String表示的行。 You cannot assign a String to an int - how would you do so?您不能将String分配给int - 您会怎么做? A String can be anything from "some text" to " a 5 0 " . String可以是从"some text"" a 5 0 " How would you convert the first and/or the second one to an int ?您如何将第一个和/或第二个转换为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... nextInt()方法需要可以表示数字的数字和/或字符序列,例如: 123-500等等...

However, you have to be aware of one significant issue - mixing nextLine() and nextInt() can lead to undesirable results.但是,您必须注意一个重要问题 - 混合使用nextLine()nextInt()会导致不良结果。 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.不会打印任何内容,因为在输入数字时,您输入例如123并按回车键,您还输入了一个换行符。 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() .然后, nextLine()将触发并扫描每个字符,直到第一个出现的换行符或文件结尾字符,这些字符显然仍然存在,但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 .您可以在此处找到扫描仪的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:不兼容的类型:int 无法转换为 String - error: incompatible types: int cannot be converted to String 错误:(136,81)错误:类型不兼容:字符串无法转换为int - Error:(136, 81) error: incompatible types: String cannot be converted to int 错误:类型不兼容:int [] []无法转换为int - error: incompatible types: int[][] cannot be converted to int BlueJ错误:“不兼容的类型:int无法转换为java.lang.String”和“不兼容的类型:java.lang.String无法转换为int” - BlueJ Error: “Incompatible types: int cannot be converted java.lang.String” AND “Incompatible types: java.lang.String cannot be converted to int” Java : 不兼容的类型; int 不能转换为字符串 - Java : incompatible types; int cannot be converted to string 不兼容的类型:字符串不能转换为int - incompatible types: String cannot be converted to int 错误:类型不兼容:int []无法转换为Integer [] - error: incompatible types: int[] cannot be converted to Integer[] 错误:类型不兼容:U无法转换为int - error: incompatible types: U cannot be converted to int 错误:类型不兼容:布尔值无法转换为int - error: incompatible types: boolean cannot be converted to int 错误:不兼容的类型:消息无法转换为 int? - Error: incompatible types: Message cannot be converted to int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM