如何在Java中将String转换为int时修复“不兼容类型”错误?

huangapple go评论108阅读模式
英文:

How to fix 'Incompatible types' error when converting String to int in Java?

问题

I'm teaching myself java and I came across this error. I'm trying to enter a number of a month and I have to use a switch case to return the "answer". I get the following error:

package javauebung5;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Type Number for name of month");
        int monthNumber = scanner.nextInt();

        String monthName = determineNameofMonth(monthNumber); // Storing the result in a String
    }

    private static String determineNameofMonth(int monthNumber) {
        switch (monthNumber) {
            case 1: return "January";

            case 2: return "February";

            case 3: return "March";

            case 4: return "April";

            case 5: return "May";

            case 6: return "June";

            case 7: return "July";

            case 8: return "August";

            case 9: return "September";

            case 10: return "October";

            case 11: return "November";

            case 12: return "December";

        }
        return "Invalid Month";
    }
}

This is the error:

Incompatible types. Found: 'java.lang.String', required: 'int'

英文:

I'm teaching myself java and I came across this error. I'm trying to enter a number of a month and I have to use a switch case to return the "answer". I get the follow error:

package javauebung5;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Type Number for name of month");
        int monthNumber = scanner.nextInt();

        monthNumber = determineNameofMonth(monthNumber);
    }

    private static String determineNameofMonth(int monthNumber) {
        switch (monthNumber) {
            case 1: return "January";

            case 2: return "February";

            case 3: return "March";

            case 4: return "April";

            case 5: return "May";

            case 6: return "June";

            case 7: return "July";

            case 8: return "August";

            case 9: return "September";

            case 10: return "October";

            case 11: return "November";

            case 12: return "December";

        }
        return "Invalid Month";
    }
}

This is the error:
> Incompatible types. Found: 'java.lang.String', required: 'int'

答案1

得分: 1

monthNumber = determineNameofMonth(monthNumber);

根据其声明和意图,determineNameofMonth返回一个String。在这里,您尝试分配给一个整数;错误消息告诉您,整数变量需要一个整数值。

可以使用类似以下方式:

String monthName = determineNameofMonth(monthNumber);

(我假设您随后想编写代码来打印出monthName,否则您对计算结果没有任何操作)。

英文:
 monthNumber = determineNameofMonth(monthNumber);

Per its declaration and intent, determineNameofMonth returns a String. Here you attempt to assign to an integer; the error message is telling you that an integer variable requires an integer value.

Use something like:

 String monthName = determineNameofMonth(monthNumber);

(I assume you'll then want to write code to print out monthName, otherwise you're doing nothing with the result of the computation).

答案2

得分: 0

monthNumber 是一个变量,它已声明为类型 int,这个类型在变量的生命周期中保持不变(在Java中无法更改变量的类型)。这意味着 monthNumber 保证只能包含 int 值,而编译器会拒绝编译代码,除非可以百分之百确保这个规则得到遵守。

determineNameofMonth(monthNumber) 是类型为 String 的,因为方法 determineNameofMonth 已经声明为返回 String 类型。String 不是 int,所以你不能这样做。

可能你想要的是:

String monthName = determineNameofMonth(monthNumber);

也就是创建一个新的变量。monthNumber 这个名称不应该表示一个 名称,否则变量的命名不当。

请注意,你其实不需要这样做,Java 已经有了 Month

import java.time.Month;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Type Number for name of month");
    int monthNumber = scanner.nextInt();
    Month m = Month.of(monthNumber);
    System.out.println("You chose: " + m);
}
英文:

>
> int monthNumber = scanner.nextInt();
> monthNumber = determineNameofMonth(monthNumber);
>

monthNumber is a variable. It has been declared to be of type int - and that is for the lifetime of that variable (you can't 're-type' a variable in java). That means monthNumber is guaranteed to never hold anything that isn't an int value, and the compiler will refuse to compile code unless it can be 100% certain that rule holds.

determineNameofMonth(monthNumber) is of type String, because the method determineNameofMonth has been declared as such. String is not an int, so, you can't do this.

Presumably you want:

String monthName = determineNameofMonth(monthNumber);

i.e. make a new variable. The name monthNumber shouldn't hold a name - then the variable is misnamed.

Note that you don't need to do this stuff, java already has Month:

import java.time.Month;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Type Number for name of month");
int monthNumber = scanner.nextInt();
Month m = Month.of(monthNumber);
System.out.println("You chose: " + m);
}

huangapple
  • 本文由 发表于 2023年5月29日 23:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定