这里是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”结尾