无法在循环

时间:2017-02-20 19:03:45

标签: java arrays file-io nested-loops

我正在尝试从.txt创建不同类型的数组(String,int ...),但我会立即面对错误消息。

 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.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at javaapplication12.JavaApplication12.main(JavaApplication12.java:27)
Java Result: 1 

这是我的代码:



package javaapplication12;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class JavaApplication12 {

    public static int num = 128;
    public static String[] name = new String[num];
    public static int[] id = new int[num];
    public static int[] year = new int[num];
    public static double[] price = new double[num];
    public static int[] sh = new int[num];
    public static int[] row = new int[num];
    public static int[] col = new int[num];

    public static void main(String[] args) throws FileNotFoundException {
        File inputFile = new File("Store.txt");
        Scanner fin = new Scanner(inputFile);
        fin.nextLine();
        fin.nextLine();

        while (fin.hasNext()) {
            for (int i = 0; i < num; i++) {
                name[i] = fin.next();
                id[i] = fin.nextInt();
                fin.next();
                fin.next();
                fin.next();
                fin.next();
                fin.next();
                year[i] = fin.nextInt();
                price[i] = fin.nextDouble();
                sh[i] = fin.nextInt();
                row[i] = fin.nextInt();
                col[i] = fin.nextInt();

            }
        }

        fin.close();
    }

}
&#13;
&#13;
&#13;

我不明白为什么会发生这种情况,因为我不熟悉编码。感谢。

以下是我正在使用的表格:

           Name      ID                Date in              Price     S  R  C
______________________________________________________________________________
             Saw     562    SAT SEP 30 00:00:00 AST 1950    37.00     2 13  1
    Tape Measure     955    TUE AUG 27 00:00:00 AST 1918    52.00    13  4  4
           Knife      25    TUE MAR 14 00:00:00 AST 2000    31.00     9 15  7
          Chisel     911    WED NOV 18 00:00:00 AST 1992    83.00     6 15  3
           Ruler     837    WED APR 19 00:00:00 AST 1978    32.00    13 13  4
            file     797    MON MAY 07 00:00:00 AST 1934    46.00    13  7  0
           Brush     190    MON SEP 07 00:00:00 AST 1959    48.00     7  3 15
         Scraper     619    WED MAR 14 00:00:00 AST 1900    36.00     3  0  0
           Level     369    SAT MAR 08 00:00:00 AST 1919    78.00    15  9  6
            Vise     487    THU OCT 06 00:00:00 AST 1904    32.00     5  3 13
            Vise     403    FRI JAN 02 00:00:00 AST 1931    87.00    10 12  2
           Brush     683    WED JUL 17 00:00:00 AST 1974    74.00     6 11  5
           Brush     821    THU DEC 26 00:00:00 AST 1929    44.00    12  6 10
    Screw Driver     246    WED SEP 03 00:00:00 AST 1947     8.00    12  0  1
           Knife     296    TUE FEB 10 00:00:00 AST 1914    72.00     9  2  0
           Knife     895    SUN MAY 17 00:00:00 AST 1903    38.00     0  8  7
          Hammer     213    SUN MAY 05 00:00:00 AST 2013    51.00     6  8 12
           Ruler      47    MON JUN 03 00:00:00 AST 1918    42.00     3  0  1
          Chisel     958    FRI OCT 09 00:00:00 AST 1981    70.00     5  7  8
            file     279    WED JAN 20 00:00:00 AST 1999    95.00     6  1  9
           Knife      90    FRI JAN 20 00:00:00 AST 2012    92.00    10  6  0
      Sand Paper     642    SUN FEB 23 00:00:00 AST 1975     6.00     6  8  0
            Vise     410    MON MAR 25 00:00:00 AST 1957    74.00     8 11  1
            file     856    MON MAY 09 00:00:00 AST 2011    20.00    12  6  8
            file     581    MON AUG 04 00:00:00 AST 1930    18.00     5 11  0
           Knife     287    WED FEB 04 00:00:00 AST 1970    96.00    15 13 10
          Wrench     905    SUN FEB 07 00:00:00 AST 1999    32.00     2  0  0
    Tape Measure     744    THU JAN 26 00:00:00 AST 1939    77.00     4 13  4
      Sand Paper     306    TUE JUN 07 00:00:00 AST 1960    82.00     1 15  5
           Brush     901    TUE FEB 08 00:00:00 AST 2000    59.00     6 10 13
           Ruler     180    FRI OCT 18 00:00:00 AST 1907    72.00    10 11  7
          Chisel     633    TUE FEB 22 00:00:00 AST 1916    55.00     9  5  5
    Tape Measure     698    WED DEC 14 00:00:00 AST 1938     8.00    12 10  6
          Wrench     315    THU OCT 22 00:00:00 AST 1925    17.00     0  0  1
          Pliers     912    TUE MAR 08 00:00:00 AST 1904     9.00     8  5  7
          Hammer     216    SAT JUN 27 00:00:00 AST 1914     9.00     1  6  4
            Vise     976    SUN OCT 04 00:00:00 AST 1998    70.00    15  9  0
          Chisel     884    THU APR 11 00:00:00 AST 1901    52.00    10 15  5
             Saw     980    SAT FEB 22 00:00:00 AST 1941    16.00    13 13 11
           Ruler     796    FRI SEP 11 00:00:00 AST 1998    88.00     5  4 15
         Scraper     313    SUN FEB 02 00:00:00 AST 1919    31.00    14  8  7
            file     885    SAT NOV 13 00:00:00 AST 1965    25.00     7  6 12
           Knife     752    TUE OCT 25 00:00:00 AST 1983    60.00     5 11  7
           Knife     470    FRI JUN 07 00:00:00 AST 1957    56.00     6 12  8
          Hammer     148    SUN FEB 28 00:00:00 AST 1965    63.00     5  8 14
            file     974    THU OCT 14 00:00:00 AST 1993    90.00     1  6 12
          Chisel     434    MON OCT 06 00:00:00 AST 1930    94.00     0  5  5
          Hammer     709    FRI SEP 14 00:00:00 AST 1945     3.00     7  6  4
          Pliers     594    SAT MAY 25 00:00:00 AST 1968    19.00     2  7 11
          Hammer     120    TUE AUG 07 00:00:00 AST 1979    40.00     3 10  2
           Ruler     396    SAT MAR 21 00:00:00 AST 1981    42.00     5  7  9
          Hammer     874    FRI MAR 22 00:00:00 AST 1935    82.00     4  8  8
          Chisel     182    SAT NOV 30 00:00:00 AST 1985    86.00    12  7  5
          Hammer     976    SUN MAR 09 00:00:00 AST 1919    83.00    11 14  0
    Tape Measure     750    FRI JUL 09 00:00:00 AST 1920    23.00     2  1  6
    Tape Measure     776    MON JUN 01 00:00:00 AST 1998    21.00    11  3  7
          Hammer     870    THU DEC 23 00:00:00 AST 1993    62.00    10 11 11
    Screw Driver     943    FRI FEB 28 00:00:00 AST 1997    84.00    10  1 13
          Hammer     689    WED FEB 28 00:00:00 AST 1934    82.00    11  8 10
           Brush     816    SAT JAN 31 00:00:00 AST 1953    51.00     1  4 14
         Scraper     500    THU SEP 24 00:00:00 AST 1931    55.00     6  5 12
           Ruler     686    THU MAY 14 00:00:00 AST 1987    17.00    13  6  8
           Brush     675    FRI JUN 09 00:00:00 AST 1922    39.00     7  0  9
      Sand Paper     938    SAT AUG 15 00:00:00 AST 1959    18.00     6  5 15
            Vise     904    WED OCT 01 00:00:00 AST 1986    35.00    12  4 15
           Level     455    FRI DEC 06 00:00:00 AST 1957    42.00    14 13  1
           Knife     999    MON MAR 24 00:00:00 AST 1930    25.00     0  0  4
         Scraper      94    MON NOV 05 00:00:00 AST 1951    22.00     8  2  4
             Saw     473    MON SEP 18 00:00:00 AST 1961    30.00     1  9 13
             Saw     140    THU JAN 27 00:00:00 AST 1921     3.00    13  2  0
    Tape Measure     401    THU AUG 22 00:00:00 AST 1985    81.00     1  4  3
    Screw Driver     923    WED JAN 26 00:00:00 AST 2005    35.00     3 14  2
       Allen Key      87    THU JUL 11 00:00:00 AST 2013    10.00     6  4  1
          Wrench     917    TUE SEP 23 00:00:00 AST 1969    35.00    12 14  8
          Wrench     367    MON AUG 05 00:00:00 AST 1929    99.00     6  3 13
             Saw     484    SAT JUN 12 00:00:00 AST 1926    36.00    10  8  8
    Screw Driver     816    WED JUL 27 00:00:00 AST 1927    13.00    13  1  0
           Ruler     476    TUE DEC 25 00:00:00 AST 1945    92.00    11  6  4
           Brush     762    FRI FEB 19 00:00:00 AST 1937    50.00     2  3  9
    Screw Driver     332    TUE SEP 06 00:00:00 AST 2011    60.00    11  7 12
           Level     849    WED AUG 25 00:00:00 AST 1937    82.00     2  6  6
           Knife     812    TUE NOV 01 00:00:00 AST 1949    58.00    13  8  4
             Saw     810    THU JAN 15 00:00:00 AST 1959     3.00     5  0  2
             Saw     385    SAT SEP 10 00:00:00 AST 1983    55.00     7  0  6
           Knife     626    TUE JUN 30 00:00:00 AST 1936     9.00    14  7 15
    Tape Measure     387    WED FEB 19 00:00:00 AST 2014    89.00    15  8  3
      Sand Paper     987    WED NOV 17 00:00:00 AST 1926    91.00     1 14 15
             Saw     880    WED MAY 13 00:00:00 AST 2009     8.00     6 14 10
      Sand Paper     932    MON JAN 09 00:00:00 AST 1995    72.00     0 14 12
            Vise     598    SUN FEB 11 00:00:00 AST 1951    97.00     3 10  0
         Scraper     369    SUN AUG 08 00:00:00 AST 1943     6.00     5 14 12
       Allen Key     167    SUN DEC 22 00:00:00 AST 1929    54.00    12 14  3
           Knife     725    SAT DEC 16 00:00:00 AST 1961    40.00    13 13 10
          Hammer     444    THU NOV 17 00:00:00 AST 1938    64.00     9 12  8
          Chisel      70    WED SEP 30 00:00:00 AST 1903    31.00     6  2 11
    Tape Measure     785    FRI APR 30 00:00:00 AST 1926    76.00     2  9 13
    Tape Measure     769    THU MAY 25 00:00:00 AST 2000    93.00    12  0 12
          Hammer     877    SAT JUL 16 00:00:00 AST 1977    68.00     1  6  0
          Wrench     802    FRI DEC 31 00:00:00 AST 1915    26.00    15 13  7
          Wrench     297    SAT JUN 12 00:00:00 AST 1976    44.00    12 15  3
            file     159    TUE MAR 21 00:00:00 AST 1967    76.00     3 10 12
       Allen Key     632    FRI MAY 14 00:00:00 AST 1971    29.00    11  6 13
           Level     651    THU FEB 12 00:00:00 AST 2004    70.00     1  6  9
          Wrench     451    MON JUL 19 00:00:00 AST 1982    68.00    10 10  4
    Tape Measure     429    FRI FEB 28 00:00:00 AST 2014    23.00     0  0  7
          Hammer     723    FRI JAN 12 00:00:00 AST 2001    86.00    13 12  5
           Level      89    WED MAY 21 00:00:00 AST 1924    36.00    10 10 14
            file     734    SAT JUL 19 00:00:00 AST 1930    88.00    10 13 14
         Scraper     489    SUN SEP 26 00:00:00 AST 1926    79.00     0  0  4
           Brush     419    FRI OCT 01 00:00:00 AST 1954    88.00    15  1  1
    Tape Measure     990    MON FEB 28 00:00:00 AST 1921    69.00     0  3  8
           Ruler      97    MON AUG 04 00:00:00 AST 1924    38.00     8  5  2
            file     944    SAT MAY 18 00:00:00 AST 1963    94.00     4  8 12
           Ruler     205    SUN DEC 22 00:00:00 AST 1912    95.00     6  0  3
          Chisel     702    TUE APR 14 00:00:00 AST 1936    98.00    15  6  6
            file     388    WED SEP 26 00:00:00 AST 1951    72.00    12  1  3
           Knife     519    WED OCT 25 00:00:00 AST 1989    97.00     4  0  4
          Pliers      90    WED OCT 31 00:00:00 AST 1928    92.00    12  3  1
            file     905    THU SEP 25 00:00:00 AST 1902    15.00     7 10  8
         Scraper     892    MON NOV 04 00:00:00 AST 1912    38.00     7  4  6
           Brush     941    WED SEP 17 00:00:00 AST 2008    52.00    10  6  3
    Screw Driver     196    TUE NOV 19 00:00:00 AST 1918    33.00     1  7  4
            Vise     675    SAT NOV 10 00:00:00 AST 1951    100.00     4  9 10
          Hammer     545    THU AUG 22 00:00:00 AST 1991    51.00    13 14  3
            Vise     240    TUE SEP 17 00:00:00 AST 1935    68.00     8  8 15
    Screw Driver     474    THU FEB 10 00:00:00 AST 1972    38.00     1  6 11
             Saw     602    MON MAY 22 00:00:00 AST 1922    65.00     8 15 14
           Knife     254    TUE JUL 11 00:00:00 AST 1961    76.00    11  1  3

2 个答案:

答案 0 :(得分:0)

你可以使用BufferReader逐行读取,你的名字有问题,有时你的名字有一个字符串,有时两个,所以你得到java.util.InputMismatchException,因为你尝试读取int in id[i] = fin.nextInt();但是这不是一个int,因为你得到这个错误,这可以帮助你:

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
    File filePath = new File("Store.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"));

    String line;
    String lineSplit[];
    int i = 0;
    while ((line = br.readLine()) != null) {
        //replace multiple spapce with one space
        line = line.replaceAll("\\s{2,}", " ").trim();
        //Split the line with space so you can get an Array :
        //like this [Saw, 562, SAT, SEP, 30, 00:00:00, AST, 1950, 37.00, 2, 13, 1]
        //or this   [Tape, Measure, 955, TUE, AUG, 27, 00:00:00, AST, 1918, 52.00, 13, 4, 4]
        lineSplit = line.split(" ");
        System.out.println(Arrays.toString(lineSplit));
        //if we notice the Name can contain one String or two so we can check get names like this :
        boolean check = false;
        try {
            Integer.parseInt(lineSplit[1]);
            check = true;
        } catch (NumberFormatException e) {
            check = false;
        }
        if (check) {
            name[i] = lineSplit[0];
            id[i] = Integer.parseInt(lineSplit[1]);
            year[i] = Integer.parseInt(lineSplit[7]);
            price[i] = Double.parseDouble(lineSplit[8]);
            sh[i] = Integer.parseInt(lineSplit[9]);
            row[i] = Integer.parseInt(lineSplit[10]);
            col[i] = Integer.parseInt(lineSplit[11]);
        } else {
            name[i] = lineSplit[0] + " " + lineSplit[1];
            id[i] = Integer.parseInt(lineSplit[2]);
            year[i] = Integer.parseInt(lineSplit[8]);
            price[i] = Double.parseDouble(lineSplit[9]);
            sh[i] = Integer.parseInt(lineSplit[10]);
            row[i] = Integer.parseInt(lineSplit[11]);
            col[i] = Integer.parseInt(lineSplit[12]);
        }
        i++;
    }
    br.close();
}

答案 1 :(得分:0)

我花了一些时间来理解代码,但最后我得到了它。

因此,您的变量num必须是文件中的总行数,并且您尝试为每个字段构建array。如果我错了,请继续阅读

如果我是对的,那么你的错误来自于使用nextLine()并相信它会跳转到文件下一行的下一个相同列值

当您使用nextLine()时,您实际上正在跳转到txt文件的下一行,该文件将从您的特定情况下的“名称”列开始,所以来吧为了引入第一行的id的int值,你实际上试图从第二行给它Name值的值

所以最好的方法是获取每一行的所有值,然后尝试拆分它们,希望你有一个逗号分隔文件,或者你可以从表格中生成一个,你从中检索你的数据

那么你将从文件中获取一个String数组,如下所示:

String[]arrayFromTheFile = new String[128];
    int i=0;
    File inputFile = new File("Store.txt");
    Scanner fin = new Scanner(inputFile);
    while (fin.hasNext()) {
    arrayFromTheFile[i] = fin.next();
    i++;
}
fin.close();

现在,只要您确定文件有128行,就构建了数组,如果没有,您将获得ArrayOutOfBoundsException这是一种全新的体验:)实际上最好做到这一点的方法是恐怖的,但正如你所说的那样,你是按照他们教给你的方式做的。

对于字段拆分以填充其他数组,您需要使用split()方法并使用字段之间可能存在的任何字符。我会留给你调查;)