简体   繁体   English

在包含字符串和双精度的外部文本文件上使用扫描仪

[英]Using scanner on an external text file containing string and doubles

Making a java program to read a text file containing both string and doubles as a price list and storing them in a Hashmap. 使一个Java程序读取包含字符串和双精度值的文本文件作为价目表,并将它们存储在Hashmap中。 Keep on getting "java.util.InputMismatchException" errors in the nextDouble() line. 在nextDouble()行中继续获取“ java.util.InputMismatchException”错误。 Code: 码:

    public static void main(String[] args) throws IOException {

        String priceList = "src/" + args[0];
        String cartOne = "src/" + args[1];
        String cartTwo = "src/" + args[2];
        Scanner priceScan = new Scanner(new File(priceList));
        priceScan.useDelimiter("    ");
        HashMap<String, Double> prices = new HashMap<String, Double>();
        priceScan.useDelimiter(" ");
        while (priceScan.hasNext()) {
            String name = priceScan.next();
            Double price = priceScan.nextDouble();
            prices.put(name, price);

        }
        priceScan.close();
        System.out.println(prices);
    }

the text file is as follows: 文本文件如下:

TV          999.99
Table         199 
Bed         499.99
Chair         45.49
Milk    3.00
Butter  2.84
Tomato 0.76
Onion 0.54
Lettuce 1.00
Ham 2.50
Bread 1.75

Your map was mapping strings to strings, when it should be mapping strings to doubles. 您的映射是将字符串映射为字符串,而应将字符串映射为双精度。

Scanner priceScan = new Scanner(new File(priceList));
HashMap<String, Double> prices = new HashMap<String, Double>();
while (priceScan.hasNext()) {
    String name = priceScan.next();
    Double price = priceScan.nextDouble();
    prices.put(name, price);
}
priceScan.close();

You are passing Double in place of String as argument. 您正在传递Double代替String作为参数。 Error is obvious. 错误很明显。

HashMap<String, String> prices = new HashMap<String, String>();

prices.put(name, price); //price is a Double

No need of calling useDelimiter() since scanner already uses spaces as delimiter to determine its tokens. 无需调用useDelimiter()因为扫描程序已经使用空格作为定界符来确定其令牌。

Also change the data type for HashMap's Value. 还要更改HashMap值的数据类型。

Here is the final program and a sample run: 这是最终程序和示例运行:

import java.io.*;
import java.util.*;

class abcd
{
public static void main(String[] args) throws IOException {

        String priceList = "file";
        //String cartOne = "src/" + args[1];
        //String cartTwo = "src/" + args[2];
        Scanner priceScan = new Scanner(new File(priceList));
        HashMap<String, Double> prices = new HashMap<String, Double>();
        while (priceScan.hasNext()) {
            String name = priceScan.next();
            Double price = priceScan.nextDouble();
            prices.put(name, price);

        }
        priceScan.close();
        System.out.println(prices);
    }
}

Sample run: 样品运行:

$ cat file 
TV          999.99
Table         199 
Bed         499.99
Chair         45.49
Milk    3.00
Butter  2.84
Tomato 0.76
Onion 0.54
Lettuce 1.00
Ham 2.50
Bread 1.75
$ javac abcd.java 
$ java abcd 
{Bed=499.99, TV=999.99, Lettuce=1.0, Table=199.0, Chair=45.49, Onion=0.54, Ham=2.5, Butter=2.84, Tomato=0.76, Milk=3.0, Bread=1.75}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM