2

这里是java新手。我正在尝试将字符串拆分为对象中的变量。正如标题所说,字符串中的最后一个字段与第一个字段一起拆分。

我的 txt 文件中的一行所有其他行都是相同的。

这是输出:

Rolling Stone#Jann Wenner#Bi-Weekly#Boston#9000
Rolling Stone#Jann Wenner#Bi-Weekly#Philadelphia#8000
Rolling Stone#Jann Wenner#Bi-Weekly#London#10000
The Economist#John Micklethwait#Weekly#New York#42000
The Economist#John Micklethwait#Weekly#Washington#29000
Nature#Philip Campbell#Weekly#Pittsburg#4000
Nature#Philip Campbell#Weekly#Berlin#6000

    Exception in thread "main" java.lang.NumberFormatException: For input string: "9000 Rolling Stone"

9000 应该是一个整数值。最后一个索引跳到下一行,因为没有 # 我该怎么办?

我相信这么多代码就足够了

    static ArrayList<String> al = new ArrayList<String>(); // Consists of lines of the text file
    static ArrayList<Magazine> bl = new ArrayList<Magazine>(); // Consists of Magazine objects

for (int i = 0; i < al.size(); i++) {
                String result[] = al.get(i).split("\\#");
                for (int j = 0; j < result.length; j++) {
                    System.out.println(result[0] + "1 " + result[1] + "2 " +  result[2] + "3 " +  result[3] + "4 "+  result[4]);
                    int num = Integer.parseInt(result[4]);

我在 split("\#") 附近有蜘蛛感觉,但我不知道是什么......

粘贴箱:http : //pastebin.com/Vp9T6aSd

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

public class Magazine {

    private String MagazineName;
    private String Publisher;
    private String Frequency;
    private String City;
    private String objectName = "mg" + loopCount;
    private int Distribution;
    private static int loopCount = 0;

    static ArrayList<String> al = new ArrayList<String>(); // Consists of lines of the text file
    static ArrayList<Magazine> bl = new ArrayList<Magazine>(); // Consists of Magazine objects

    private static void readData() throws FileNotFoundException {
        java.io.File file = new java.io.File(
                "/Users/henrydang/Desktop/Zines.txt");
        Scanner sc = new Scanner(file);
        while (sc.hasNext()) {
            sc.useDelimiter("\\n");
            String line = sc.next();
            System.out.println(line);
            al.add(line);
            Magazine objectName;

            for (int i = 0; i < al.size(); i++) {
                String result[] = al.get(i).split("#");
                for (int j = 0; j < result.length; j++) {
                    System.out.println(result[0] + "1 " + result[1] + "2 " +  result[2] + "3 " +  result[3] + "4 "+  result[4]);
                    int num = Integer.parseInt(result[4]);

                    objectName = new Magazine();
                    objectName.setMagazine(result[0]);
                    objectName.setPublisher(result[1]);
                    objectName.setFrequency(result[2]);
                    objectName.setCity(result[3]);
                    objectName.setDistribution(num);
                    bl.add(objectName);             
                }

            }
            loopCount++;
            sc.close();

        }
    }

    public Magazine() {

    }

    public void setMagazine(String name) {
        this.MagazineName = name;
    }

    public void setPublisher(String name) {
        this.Publisher = name;
    }

    public void setFrequency(String name) {
        this.Frequency = name;
    }

    public void setCity(String name) {
        this.City = name;
    }

    public void setDistribution(int num) {
        this.Distribution = num;
    }

    public String getMagazine() {
        return MagazineName;
    }

    public String getPublisher() {
        return Publisher;
    }

    public String getFrequency() {
        return Frequency;
    }

    public String getCity() {
        return City;
    }

    public int getDistribution() {
        return Distribution;
    }

    public static void main(String args[]) throws FileNotFoundException {
        readData();
    }

}

编辑:更多信息已解决:文本文件以 /r/n 而不是“/n”结尾

4

2 回答 2

0

所以亨利...

您的代码有几个问题:

  1. while (sc.hasNext()){}sc.close() 在循环内被调用。
  2. 在 sc.hasNext() 内部调用 sc.setDelimiter() 有点阴暗,但我猜它.next()在设置分隔符后调用它会起作用。
  3. 我们发现问题出在输入文件本身,而不是您的代码本身。如果您正在编写一些特定于平台的代码(例如使用\n分隔符),请务必仔细检查您的文件。

这是您的代码,我会以正确/理想的方式为您的代码使用扫描仪:

private static void readData() throws FileNotFoundException {  
  java.io.File file = new java.io.File("Zines.txt");  
  Scanner sc = new Scanner(file);  
  while (sc.hasNextLine()) {  
    String line = sc.nextLine();  
  }  
  sc.close();  
}

希望这一切都有帮助!

编辑:忽略下面的评论对话。我们没有生气,我重做了答案。

于 2013-01-13T13:09:40.683 回答
0

我想你可以试试:

String result[] = al.get(i).split("#\\s*");

每当出现“#”时,它将分隔子字符串。

于 2013-01-13T13:11:00.020 回答