这里是 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 应该是一个整数值。最后一个索引跳到下一行,因为没有# what should I do?
我相信这么多代码就足够了
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("\#") 的地方有一种蜘蛛感觉,但我不知道是什么......
Pastebin: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"结尾
最佳答案
所以亨利...
你的代码有几个问题:
- sc.close() 在
while (sc.hasNext()){}
循环中被调用。 - 在 sc.hasNext() 中调用 sc.setDelimiter() 有点可疑,但我猜它在设置分隔符后调用
.next()
时有效。 - 我们发现问题出在输入文件本身,而不是您的代码本身。如果您正在编写一些特定于平台的代码(例如使用
\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();
}
希望对您有所帮助!
编辑:忽略下面的评论对话。我们没有生气,我重做了答案。
关于java - 当我按 # 拆分字符串的 ArrayList 时,最后一个字段不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14303739/