存储文件中的行号和值的Java
我有一组像这样的数据存储文件中的行号和值的Java,java,Java,我有一组像这样的数据 1:2:3:4:5 6:7:8:9:10 我已经设法使用数组列表来存储使用分隔符“:”的信息。 但是,我想将它们的行号信息一起存储在数组列表中 class test { String items; String linenumber; } Example: test(1,1) test(2,1) test(6,2) test(7,2) 这是我目前的代码 Scanner fileScanner = new Scanner
1:2:3:4:5
6:7:8:9:10
我已经设法使用数组列表来存储使用分隔符“:”的信息。
但是,我想将它们的行号信息一起存储在数组列表中
class test
{
String items;
String linenumber;
}
Example:
test(1,1)
test(2,1)
test(6,2)
test(7,2)
这是我目前的代码
Scanner fileScanner = new Scanner(new File(fname));
fileScanner.useDelimiter("\n");
int counter = 0; String scounter;
String test;
String events;
while(fileScanner.hasNext())
{
events = fileScanner.next();
scounter = Integer.toString(counter);
Base obj = new Base(scounter, events);
baseArrayList.add(obj);
}
fileScanner.close();
我尝试使用分隔符“\n”,然后尝试拆分字符串,但效果不是很好。
如有任何建议,将不胜感激
public void Base_Seperator()
{
String temp, temp2;
String[] split;
String days, events;
for(int i = 0; i < baseArrayList.size(); i++)
{
temp = baseArrayList.get(i).events;
temp2 = baseArrayList.get(i).days;
split = temp.split(":");
}
}
public void Base_separator()
{
字符串temp,temp2;
字符串[]拆分;
串天、串事件;
对于(int i=0;i
尽管@Alex的答案中的代码可能会解决您的问题,但您的尝试几乎接近您想要/需要的。现在,您只需要创建测试
实例并将它们存储在容器中,通常是列表
。我将添加必要的代码以从您的代码开始:
//it is better to return the List instead of declaring it as a static field
public List<Test> Base_Seperator() {
//try to declare variables in the narrower scope
//String temp, temp2;
//String[] split;
//String days, events;
//this variable must be recognized in all the paths of this method
List<Test> testList = new ArrayList<Test>();
for(int i = 0; i < baseArrayList.size(); i++) {
//these variables should only work within the for statement
String temp = baseArrayList.get(i).events;
String temp2 = baseArrayList.get(i).days;
String[] split = temp.split(":");
//you have splitted the String by :
//now you have every element between : as an item stored in split array
//go through each one and create a new Test instance
//first, let's create the lineNumber variable as String
String lineNumber = Integer.toString(i+1);
//using enhanced for to go through these elements
for (String value : split) {
//now, let's create Test instance
Test test = new Test(value, lineNumber);
//store the instance in testList
testList.add(test);
}
}
//now just return the list with the desired values
return testList;
}
如果需要使用接口/抽象类的不同实现,则只需更改对象初始化(希望如此)
更多信息:
看起来您想在
Test
实例中存储days
而不是lineNumber
:
//comment this line
//Test test = new Test(value, lineNumber);
//use this one instead
Test test = new Test(value, days);
尽管@Alex答案中的代码可能会解决您的问题,但您的尝试几乎接近您想要/需要的。现在,您只需要创建
测试
实例并将它们存储在容器中,通常是列表
。我将添加必要的代码以从您的代码开始:
//it is better to return the List instead of declaring it as a static field
public List<Test> Base_Seperator() {
//try to declare variables in the narrower scope
//String temp, temp2;
//String[] split;
//String days, events;
//this variable must be recognized in all the paths of this method
List<Test> testList = new ArrayList<Test>();
for(int i = 0; i < baseArrayList.size(); i++) {
//these variables should only work within the for statement
String temp = baseArrayList.get(i).events;
String temp2 = baseArrayList.get(i).days;
String[] split = temp.split(":");
//you have splitted the String by :
//now you have every element between : as an item stored in split array
//go through each one and create a new Test instance
//first, let's create the lineNumber variable as String
String lineNumber = Integer.toString(i+1);
//using enhanced for to go through these elements
for (String value : split) {
//now, let's create Test instance
Test test = new Test(value, lineNumber);
//store the instance in testList
testList.add(test);
}
}
//now just return the list with the desired values
return testList;
}
如果需要使用接口/抽象类的不同实现,则只需更改对象初始化(希望如此)
更多信息:
看起来您想在
Test
实例中存储days
而不是lineNumber
:
//comment this line
//Test test = new Test(value, lineNumber);
//use this one instead
Test test = new Test(value, days);
尽管@Alex答案中的代码可能会解决您的问题,但您的尝试几乎接近您想要/需要的。现在,您只需要创建
测试
实例并将它们存储在容器中,通常是列表
。我将添加必要的代码以从您的代码开始:
//it is better to return the List instead of declaring it as a static field
public List<Test> Base_Seperator() {
//try to declare variables in the narrower scope
//String temp, temp2;
//String[] split;
//String days, events;
//this variable must be recognized in all the paths of this method
List<Test> testList = new ArrayList<Test>();
for(int i = 0; i < baseArrayList.size(); i++) {
//these variables should only work within the for statement
String temp = baseArrayList.get(i).events;
String temp2 = baseArrayList.get(i).days;
String[] split = temp.split(":");
//you have splitted the String by :
//now you have every element between : as an item stored in split array
//go through each one and create a new Test instance
//first, let's create the lineNumber variable as String
String lineNumber = Integer.toString(i+1);
//using enhanced for to go through these elements
for (String value : split) {
//now, let's create Test instance
Test test = new Test(value, lineNumber);
//store the instance in testList
testList.add(test);
}
}
//now just return the list with the desired values
return testList;
}
如果需要使用接口/抽象类的不同实现,则只需更改对象初始化(希望如此)
更多信息:
看起来您想在
Test
实例中存储days
而不是lineNumber
:
//comment this line
//Test test = new Test(value, lineNumber);
//use this one instead
Test test = new Test(value, days);
尽管@Alex答案中的代码可能会解决您的问题,但您的尝试几乎接近您想要/需要的。现在,您只需要创建
测试
实例并将它们存储在容器中,通常是列表
。我将添加必要的代码以从您的代码开始:
//it is better to return the List instead of declaring it as a static field
public List<Test> Base_Seperator() {
//try to declare variables in the narrower scope
//String temp, temp2;
//String[] split;
//String days, events;
//this variable must be recognized in all the paths of this method
List<Test> testList = new ArrayList<Test>();
for(int i = 0; i < baseArrayList.size(); i++) {
//these variables should only work within the for statement
String temp = baseArrayList.get(i).events;
String temp2 = baseArrayList.get(i).days;
String[] split = temp.split(":");
//you have splitted the String by :
//now you have every element between : as an item stored in split array
//go through each one and create a new Test instance
//first, let's create the lineNumber variable as String
String lineNumber = Integer.toString(i+1);
//using enhanced for to go through these elements
for (String value : split) {
//now, let's create Test instance
Test test = new Test(value, lineNumber);
//store the instance in testList
testList.add(test);
}
}
//now just return the list with the desired values
return testList;
}
如果需要使用接口/抽象类的不同实现,则只需更改对象初始化(希望如此)
更多信息:
看起来您想在
Test
实例中存储days
而不是lineNumber
:
//comment this line
//Test test = new Test(value, lineNumber);
//use this one instead
Test test = new Test(value, days);
首先,您不需要在
test
对象中保留行号信息,因为它可以从保存它们的ArrayList中推断出来。如果必须这样做,则应将其更改为int
。所以
class test
{
ArrayList items<Integer>;
int linenumber;
public test(int line, String[] input){
items=new ArrayList();
linenumber=line;
//populate with the line read by the Scanner
for(int i=0; i<input.lenth; i++)
items.add(Integer.parseInt(input[i]));
}
}
这里我使用
test
来存储您读取的对象,我不确定Base
应该是什么。首先,您不需要在test
对象中保留行号信息,因为它可以从保存它们的ArrayList中推断出来。如果必须这样做,则应将其更改为int
。所以
class test
{
ArrayList items<Integer>;
int linenumber;
public test(int line, String[] input){
items=new ArrayList();
linenumber=line;
//populate with the line read by the Scanner
for(int i=0; i<input.lenth; i++)
items.add(Integer.parseInt(input[i]));
}
}
这里我使用
test
来存储您读取的对象,我不确定Base
应该是什么。首先,您不需要在test
对象中保留行号信息,因为它可以从保存它们的ArrayList中推断出来。如果必须这样做,则应将其更改为int
。所以
class test
{
ArrayList items<Integer>;
int linenumber;
public test(int line, String[] input){
items=new ArrayList();
linenumber=line;
//populate with the line read by the Scanner
for(int i=0; i<input.lenth; i++)
items.add(Integer.parseInt(input[i]));
}
}
这里我使用
test
来存储您读取的对象,我不确定Base
应该是什么。首先,您不需要在test
对象中保留行号信息,因为它可以从保存它们的ArrayList中推断出来。如果必须这样做,则应将其更改为int
。所以
class test
{
ArrayList items<Integer>;
int linenumber;
public test(int line, String[] input){
items=new ArrayList();
linenumber=line;
//populate with the line read by the Scanner
for(int i=0; i<input.lenth; i++)
items.add(Integer.parseInt(input[i]));
}
}
在这里,我使用
test
来存储您阅读的对象,我不确定Base
应该是什么。需要一个Java Bean/构造来将日期和项目放在一起。下面的代码将读取文本文件。每一行都将转换为一个列表,应用程序最终将在其中正确填充List DayItems集合
public class DayItem {
private int day;
private String item;
public int getDay() {
return day;
}
public void setDay(final int day) {
this.day = day;
}
public String getItem() {
return item;
}
public void setItem(final String item) {
this.item = item;
}
}
和主代码
public class ReadFile {
private static final List<DayItem> dayItems = new ArrayList<DayItem>();
public static void main(String args[]) throws FileNotFoundException{
final BufferedReader bufferReader = new BufferedReader(new FileReader("items.txt"));
int lineNumber=0;
try
{
String currentLine;
while ((currentLine = bufferReader.readLine()) != null) {
lineNumber++;
List<String> todaysItems = Arrays.asList(currentLine.split(":"));
addItems(todaysItems,lineNumber);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void addItems(final List<String> todaysItems,final int day){
int listSize = todaysItems.size();
for(int i=0;i<listSize;i++){
String item = todaysItems.get(i);
DayItem dayItem = new DayItem();
dayItem.setDay(day);
dayItem.setItem(item);
dayItems.add(dayItem);
}
}
}
公共类读取文件{
private static final List dayItems=new ArrayList();
公共静态void main(字符串args[])引发FileNotFoundException{
final BufferedReader bufferReader=new BufferedReader(new FileReader(“items.txt”);
int lineNumber=0;
尝试
{
串电流线;
而((currentLine=bufferReader.readLine())!=null){
lineNumber++;
List todaysItems=Arrays.asList(currentLine.split(“:”);
附加项(今天的站点、行号);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
专用静态无效附加项(今天的最终列表站点,最后一个整数日){
int listSize=todaysItems.size();
对于(int i=0;i需要一个Java Bean/构造来将日期和项目保存在一起。下面的代码将读取文本文件。每一行将转换为一个列表,应用程序将在其中正确填充List DayItems集合
public class DayItem {
private int day;
private String item;
public int getDay() {
return day;
}
public void setDay(final int day) {
this.day = day;
}
public String getItem() {
return item;
}
public void setItem(final String item) {
this.item = item;
}
}
及