我正在尝试使用java.util.Scanner逐个字符地读取单行文件.但是我得到了这个例外":
Exception in thread "main" java.util.InputMismatchException: For input string: "contents of my file"
at java.util.Scanner.nextByte(Scanner.java:1861)
at java.util.Scanner.nextByte(Scanner.java:1814)
at p008.main(p008.java:18) <-- line where I do scanner.nextByte()
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public static void main(String[] args) throws FileNotFoundException {
File source = new File("file.txt");
Scanner scanner = new Scanner(source);
while(scanner.hasNext()) {
System.out.println((char)scanner.nextByte());
}
scanner.close()
}
Run Code Online (Sandbox Code Playgroud)
有没有人对我可能做错了什么有任何想法?
编辑:我意识到我写了hasNext()而不是hasNextByte().但是,如果我这样做,它不打印任何东西.
sry about my english :)
我是Java编程的新手,我遇到了Scanner的问题.我需要读取一个Int,显示一些东西,然后读取一个字符串,所以我使用sc.nextInt(); 显示我的东西showMenu(); 然后尝试读取字符串palabra = sc.nextLine();
有人告诉我,我需要使用sc.nextLine(); 在sc.nextInt()之后; 但我不明白为什么你要这样做:(
这是我的代码:
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int respuesta = 1;
showMenu();
respuesta = sc.nextInt();
sc.nextLine(); //Why is this line necessary for second scan to work?
switch (respuesta){
case 1:
System.out.println("=== Palindromo ===");
String palabra = sc.nextLine();
if (esPalindromo(palabra) == true)
System.out.println("Es Palindromo");
else
System.out.println("No es Palindromo");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
非常适合你的时间和帮助:D
我想从控制台读取几个数字.我想这样做的方法是让用户输入一个由空格分隔的数字序列.代码执行以下操作:
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int i = sc.nextInt();
//... do stuff with i ...
}
Run Code Online (Sandbox Code Playgroud)
问题是,当到达新行时如何停止(同时保持上面易于阅读的代码)?在上面的参数中添加!hasNextLine()会使其立即退出循环.一种解决方案是读取整行并解析数字,但我认为有点破坏了hasNextInt()方法的目的.
我已经创建了一个Scanner输入,System.in以便我可以从控制台获取输入.
Scanner scanner = new Scanner(System.in, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
当我做
String s = scanner.next();
Run Code Online (Sandbox Code Playgroud)
然后????? ???在控制台中输入,字符串的值变为???? ???.
控制台能够显示Unicode字符,但为什么我不能读它们?
我之前问了一个问题,从那以后我编辑了我的代码,但现在我的代码不会停止,当我读完它不会停止.
public class Done {
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
ArrayList<String> sal = new ArrayList<String>();
int count = 0;
while (true){
sal.add(kb.next());
if (sal.equals("done"))
break;
count++;
}
display(sal);
displayb(sal);
}
public static void display(ArrayList<String> sal){
for (int i=0; i<sal.size(); i++)
System.out.print(sal.get(i)+ " ");
System.out.println();
}
public static void displayb(ArrayList<String> sal){
for (int z = sal.size(); z >= 1; z--)
System.out.print(sal.get(z-1) + " ");
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
当我输入短语"done"时,我的代码不会停止.谁知道我可能做错了什么?
我正面临 Scanner.hasNextLine() 这个奇怪的问题。我不使用 Scanner 类,但我习惯使用 Scanner.hasNextLine() 作为循环的条件来连续获取用户的输入,但在这部分代码中它总是返回 false !
public static void main(String[] args) throws IOException {
String s = "";
Scanner ssc = new Scanner(System.in);
Client cli = new Client();
cli.HLO();
Thread t = new Thread(new ThreadMessage(cli));//Thread that asks user for input using Scanner and closing it then
t.start();
boolean b = ssc.hasNextLine();
while (true) {
b = ssc.hasNextLine();
System.out.println(b);
if (b) {
s = ssc.nextLine();
System.out.println("you wrote" + s);
cli.dp.setData(s.getBytes());
cli.ds.send(cli.dp);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和输入一样,我只有一个带有单词 false 的行循环
有没有人知道它为什么会这样? …
我创建了一个带有一些按钮的 JFrame 窗口,我想让它在用户单击特定按钮时启动一个方法,该方法将使用扫描仪对象从用户那里获取信息。
出于某种原因,在我从框架中单击该按钮后,该方法会启动,但我无法输入任何信息。
有没有办法在同一个应用程序中使用 JFrame 和 Scanner 类?
使用下面的代码,我可以使用/不使用扫描仪关闭方法生成相同的输出.
import java.util.Scanner;
class CheckPassFail
{
//Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise.
static int k;
static String str;
public static void main(String arg[])
{
System.out.println("Enter the marks : ");
Scanner typein = new Scanner(System.in);
str = typein.nextLine();
typein.close(); //Scanner Close
k = Integer.parseInt(str);
if(k>=50)
{
System.out.println("PASS");
}
else
{
System.out.println("FAIL");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个小算法,这是其中的一部分。
如果用户输入非整数值,我想输出一条消息并让用户再次输入一个数字:
boolean wenttocatch;
do
{
try
{
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);
Run Code Online (Sandbox Code Playgroud)
我得到了一个永无止境的循环,我不知道为什么。
如何识别用户是否输入了一些非整数?
如果用户输入一个非整数,我如何要求用户再次输入?
更新
当我打印异常时,我收到“InputMismatchException”,我该怎么办?
java input try-catch java.util.scanner inputmismatchexception
我正在构建一个程序来询问乘法,我想设置一个计时器来强制该人在给定时间内给出答案:
目前,案例1可以完成,但2不是,我正在考虑return;从方法内部的方式,如线程或其他东西,我不知道如何
所以我面临一个问题,如果一个Scanner是开放的,等待输入,如何阻止它?我已经尝试将它放在一个线程和interrupt()它或使用boolean作为标志,但它不会停止Scanner
class Multiplication extends Calcul {
Multiplication() { super((nb1, nb2) -> nb1 * nb2); }
@Override
public String toString() { return getNb1() + "*" + getNb2(); }
}
abstract class Calcul {
private int nb1, nb2;
private boolean valid;
private boolean inTime = true;
private boolean answered = false;
private BiFunction<Integer, Integer, Integer> function;
Calcul(BiFunction<Integer, Integer, Integer> f) {
this.nb1 = new Random().nextInt(11); …Run Code Online (Sandbox Code Playgroud)