1
嗨,大家好,我是Java的初學者,我試圖從文件中讀取一些數據,但是,當它試圖讀取雙重的價值,我發生了錯誤無法弄清楚爲什麼。這是我的代碼:讀雙使用掃描文件,java.util.InputMismatchException
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)
謝謝你,我會試試! –
讓我知道它是否工作@TiagoLima –
它工作完美,只有一件事是缺少哪一行是「i ++;」。萬分感謝! :) –