英文:
The method next() is undefined for the type String
问题
我是新手程序员,我正在尝试从一个方法中调用一个扫描器,并在编译之前收到错误,我在这个论坛或其他地方找不到我能理解的答案。
public class BL {
static double outcome = 0;
private static String input;
private static void main() {
Scanner input = new Scanner(System.in);
}
public static void sort() {
**input = input.next();** // 错误出现在 .next() 下面
}
}
非常感谢!
英文:
I'm new to programming and I'm trying to call a scanner from a method- and receiving the error before compiling, I could not find an answer to this in this forum or outplace, that I could understand
public class BL {
static double outcome = 0;
private static String input;
private static void main() {
Scanner input = new Scanner(System.in);
}
public static void sort() {
**input= input.next();** // the error is under .next()
}
Thanks upfront!
答案1
得分: 1
我猜你想要
public static void sort() {
Scanner in = new Scanner(System.in);
input = in.next(); //或者使用in.nextLine()来获取整行输入
}
Scanner
是用于从键盘读取输入的类。它是拥有 next()
方法的类,该方法获取用户输入(直到找到空格或新行)并将其转换为 String
。
英文:
I guess you want
public static void sort() {
Scanner in = new Scanner(System.in);
input = in.next(); //or in.nextLine() for the whole line
}
The Scanner
is the class that reads input from the keyboard. It is the class who has the method next()
, that retrieves the input of the user (until it finds a space or a new line) converted in a String
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论