- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
大家好,我是 Java 初学者,我试图从文件中读取一些数据,但是当它尝试读取 double 值时发生错误,我不明白为什么。这是我的代码:
package bestcombo;
import java.io.*;
import java.util.Scanner;
public class Componentes
{
String name = "";
Lojas[] stores = new Lojas[8];
public void ComponenteNome(String nome)
{
name = nome;
}
public void InicializeStores()
{
for (int i = 0; i < 8; i++)
{
stores[i] = new Lojas();
}
}
public void InserirInformacao() throws IOException
{
int i = 0, val = 0, quant = 0;
double price = 0;
String FileName = "";
Scanner ReadKeyboard = new Scanner(System.in), ReadFile = null;
InicializeStores();
System.out.println("File:\n");
FileName = ReadKeyboard.nextLine();
ReadFile = new Scanner(new File(FileName));
while(ReadFile.hasNext())
{
ReadFile.useDelimiter(":");
val = ReadFile.nextInt();
ReadFile.useDelimiter(":");
price = ReadFile.nextDouble();
ReadFile.useDelimiter("\\n");
quant = ReadFile.nextInt();
stores[i].LojaValor(val);
stores[i].LojaQuantidade(quant);
stores[i].LojaPreco(price);
}
}
}
这是我文件中的数据:
1:206.90:1
2:209.90:1
3:212.90:1
4:212.90:1
5:213.90:1
6:224.90:1
7:229.24:1
8:219.00:1
这是错误
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at bestcombo.Componentes.InserirInformacao(Componentes.java:34)
at bestcombo.BestCombo.main(BestCombo.java:13)
最佳答案
从文件读取时,我建议使用 getNextLine(),它可以使代码和值提取变得更加简单。另外,您读入的数据文件是用冒号分隔的,这使得提取值变得非常容易。尝试使用这个 while 循环来代替
while(ReadFile.hasNextLine())
{
String inputOfFile = ReadFile.nextLine();
String[] info = inputOfFile.split(":");
try{
if(info.length == 3)
{
val = Integer.parseInt(info[0]);
price = Double.parseDouble(info[1]);
quant = Integer.parseInt(info[2]);
stores[i].LojaValor(val);
stores[i].LojaQuantidade(quant);
stores[i].LojaPreco(price);
i++;
}
else
{
System.err.println("Input incorrectly formatted: " + inputOfFile);
}
}catch (NumberFormatException e) {
System.err.println("Error when trying to parse: " + inputOfFile);
}
}
我的猜测是您正在读取的文件中有一个额外的换行符或其他内容。上面应该能够很容易地处理你的文件。此实现的另一个优点是,即使您遇到文件中数据格式不正确的位置,它也会继续从文件中读取。
关于java - 使用扫描仪从文件中读取 double 值,java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41681907/
这个问题已经有答案了: Scanner double value - InputMismatchException (2 个回答) 已关闭2 年前。 为什么 Java 在使用 Scanner.next
我正在实现一个驱动程序来测试决策树,但遇到输入不匹配异常。我可以做什么来解决这个问题? 这是我的代码: import java.io.FileNotFoundException; /** * Loa
static long arrayMaxMin[]; public static void main(String[] args) { try{ Scanner in = new
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (23 个回答) 5年前关闭。 impor
简介 我在捕获 InputMismatchException 时遇到问题。当我搜索 InputMismatchException 时,我已经阅读了该网站上出现在第一页上的所有线程。我在阅读这些帖子时找
while(inputbook.hasNext()){ id = inputbook.nextInt(); name = inputbook.next(); year = in
我有这个代码: import java.util.Scanner; public class Maggiore3Valori { public static void main(String[
public static void main(String[] args) { try { Scanner scan = new Scanner(new Fi
当输入 55.5 作为第一个提示的答案时,我收到以下错误。输入 50 时我没有收到错误消息。 Exception in thread "main" java.util.InputMismatchExc
为什么我遇到了 inputMismatchException?当输入仅为整数时,结果会很好,但是当我使用双输入时,编译器会抛出 inputMismatchException。 public stati
我有一个任务: 5-8)(求最高分)编写一个程序,提示用户输入学生人数以及每个学生的姓名和分数,最后显示分数最高的学生的名字。 当我运行此代码并输入“john”等名称时,它运行良好,但当我尝试输入“J
我尝试以最简单的方式使用扫描仪: 代码: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in
我试图提示用户输入 x 坐标,但是当我输入十进制数(例如 2.1)作为输入时,这会导致问题。如何解决该问题? import java.util.Scanner; public cl
所以我一直在做一项任务,要求我们建立一个“患者经理”,根据紧急级别确定首先治疗的患者。我遇到的主要问题是,当用户在紧急栏中输入数字以外的内容时,它会使程序崩溃并给我一个 InputMismatchEx
public static void main(String[] args) throws FileNotFoundException { File file = new File("data
我写了下面这段Java代码。当运行它并输入任何值(定义的值,例如 latte,或任何其他值,例如 az 整数)时,我得到一个 InputMismatchException。 据我所知,此异常意味着输入
我希望用户输入一行文本,只要 x 不等于 numOfContestans 的值。当我运行代码时,我收到一个 InputMismatchException。有人知道如何解决这个错误吗? try {
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 7 年前。 我有文本文
我无法找到错误的根源。我所做的就是从文件中读取文本 public static void main(String[] args) throws Exception { int T; S
我在将非整数输入整数字段时遇到问题。我只是采取预防措施,这样如果其他人使用/处理我的程序,他们就不会得到此 InputMismatchException。 当我在 input 变量中输入非数字字符时,
我是一名优秀的程序员,十分优秀!