我正在尝试使用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 scanner = new Scanner();
int number = 1;
do
{
try
{
option = scanner.nextInt();
}
catch (InputMismatchException exception)
{
System.out.println("Integers only, please.");
}
}
while (number != 0);
Run Code Online (Sandbox Code Playgroud)
尽管存在异常处理,但在给出非整数输入时,此代码将进入无限循环.它不是Scanner暂停在下一次迭代中收集输入,而是继续抛出InputMismatchExceptions直到程序被杀死.
扫描整数(或其他类型,我猜)输入,丢弃无效输入并正常继续循环的最佳方法是什么?
我正在用Java编写一个简单的程序,它需要从文本文件中读取数据.但是,我在计算线路时遇到了麻烦.对于简单的谷歌搜索来说,这个问题看起来很通用,但我甚至可能找不到合适的东西.
我正在学习的教科书建议,要计算文本文件中的行数,你应该这样做:
public static int[] sampleDataArray(String inputFile) throws IOException
{
File file = new File(inputFile);
Scanner inFile = new Scanner(file);
int count = 0;
while (inFile.hasNext())
count++;
int[] numbersArray = new int[count];
inFile.reset();
for (int i = 0; i < count; i++)
{
numbersArray[i] = inFile.nextInt();
}
inFile.close();
return numbersArray;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这while (inFile.hasNext())条线是问题所在.我认为这hasNext()是无限的运行.我在代码中使用的数据文件肯定具有有限数量的数据行.
我该怎么办?
我想做 -
BufferedReader br = new BuffereReader(file);
Scanner s = new Scanner(br);
sys.out(s.next());
sys.out(buffReader.readLine());
Run Code Online (Sandbox Code Playgroud)
我期望发生的是现在在文件上有两个独立的读者指向不同的地方.但是,buffReader在readLine上返回null,而扫描程序似乎工作正常.我有可能拥有像我想要的2位读者吗?
到目前为止我有这个:
public double checkValueWithin(int min, int max) {
double num;
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();
while (num < min || num > max) {
System.out.print("Invalid. Re-enter number: ");
num = reader.nextDouble();
}
return num;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
public void askForMarks() {
double marks[] = new double[student];
int index = 0;
Scanner reader = new Scanner(System.in);
while (index < student) {
System.out.print("Please enter a mark (0..30): ");
marks[index] = (double) checkValueWithin(0, 30);
index++;
}
}
Run Code Online (Sandbox Code Playgroud)
当我测试这个,它不能采取双数,我得到这个消息:
Exception in thread …Run Code Online (Sandbox Code Playgroud) 输入格式
读取一些未知的n行输入,stdin(System.in)直到你达到EOF; 每行输入都包含一个非空字符串.
输出格式
对于每一行,打印行号,后跟一个空格,然后输入作为输入的行内容:
样本输出
Hello world
I am a file
Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案.问题是我无法继续进行EOF.但输出只是:
Hello world
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public class Solution {
public static void main(String[] args) {
check(1); // call check method
}
static void check(int count) {
Scanner s = new Scanner(System.in);
if(s.hasNext() == true) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
check(count);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在用图论编写我的学士论文,并使用java扫描器将带边的txt文件转换为我的Java类图.我的txt文件如下所示:
1 2 72 3
2 3 15 98
4 7 66 49
5 6 39 48
6 9 87 97
8 13 31 5
整数排序为:源顶点,汇点顶点,成本,容量.
我的代码如下:
Graph graph = new Graph(false);
File f = new File("Filepath");
Scanner in = new Scanner(f);
while (in.hasNextLine())
{
for (int i =1; i<= numberEdges; i++)
{
String s = in.nextLine();
try (Scanner inscan = new Scanner(s)) {
while (inscan.hasNext())
{
int source = inscan.nextInt();
int sink = inscan.nextInt();
double cost =inscan.nextDouble();
double capacity …Run Code Online (Sandbox Code Playgroud) 我正在构建一个程序来询问乘法,我想设置一个计时器来强制该人在给定时间内给出答案:
目前,案例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)