I am complete beginner. Have a data input file with multiple lines like this
我是初学者。有一个像这样的多行数据输入文件
name, number, number,...
I can read the line as a string but need to input the values into an array. How do I do that. I've spent 2 days trying to figure it out.
我可以将该行作为字符串读取,但需要将值输入到数组中。我怎么做。我花了两天时间试图解决这个问题。
4 个解决方案
#1
1
As you have mentioned each line has words separated with comma. So you can read the lines and then split the lines to get the array for each line. You may want to add these arrays to another collection such as an ArrayList:
正如您所提到的,每一行都有用逗号分隔的单词。因此,您可以读取线条,然后分割线条以获得每条线条的数组。您可能希望将这些数组添加到另一个集合,例如ArrayList:
So I am assuming you have the file reading logic and each line you are getting in a string called line
. You need to put these pieces at the right places in your code:
所以我假设您有文件读取逻辑,并且每行都在一个名为line的字符串中。您需要将这些部分放在代码中的正确位置:
// collection to hold array of words in each line
ArrayList<String[]> listOfWords = new ArrayList<String[]>();
// split your line into an array of words
String wordsInLine[] = line.split("[ ,]+");
// add words array to your collection
listofWords.add(wordsInLine);
#2
1
Things to check out:
要检查的事情:
But particularly, String.split()
:
但特别是String.split():
String list = "first second third";
String[] items = list.split(" "); // = { "first", "second", "third" };
Or with regular expressions:
或者使用正则表达式:
String list = "first, second, third";
String[] items = list.split("[ ,]+"); // = { "first", "second", "third" };
Then you have things like StringTokenizer
, which can do it all, e.g.:
那么你有像StringTokenizer这样的东西,可以做到这一切,例如:
String values = "name, 1, 2, 3";
String name = null;
List<Integer> numbers = new ArrayList<Integer>();
StringTokenizer tokenizer = new StringTokenizer(value, " ,");
if (tokenizer.hasMoreTokens()) // first token is name
name = tokenizer.nextToken();
else
throw new Exception("No name!");
while (tokenizer.hasMoreTokens()) // rest are numbres
numbers.add(Integer.parseInt(tokenizer.nextToken());
Or you can do fun stuff with Scanners:
或者您可以使用扫描仪做有趣的事情:
String values = "name, 1, 2, 3";
Scanner scanner = new Scanner(values);
scanner.useDelimiter("[ ,]+"); // regular expression, 1 or more spaces/commas
String name = null;
List<Integer> numbers = new ArrayList<Integer>();
if (scanner.hasNext()) // first token is name
name = scanner.next();
else
throw new Exception("No name!");
while (scanner.hasNextInt()) // rest are numbers
numbers.add(scanner.nextInt());
There's a lot of ways to skin this cat.
有很多方法可以给这只猫上皮。
#3
0
Arrays are fixed sized, so you would have to know how many elements you want to add via a loop. I would recommend using an ArrayList.
数组是固定大小的,因此您必须知道要通过循环添加多少元素。我建议使用ArrayList。
This should help: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
这应该有所帮助:http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
ArrayList<String> listOfStrings = new ArrayList<String>(); // instantiating the object
listOfStrings.add(STRING_VARIABLE) // adding onto the ArrayList.
listOfStrings.get(INDEX_NUMBER) // gets the actual String object, using the index number
#4
0
String strFromFile = "name,number,number,";
if(strFromFile.contains(","))
{
String[] splitInput = strFromFile.split(",");
String part1 = splitInput[0]; //gives you name
String part2 = splitInput[1]; // gives you number
.....
}
else
{
throw new IllegalArgumentException("String " + strFromFile + " does not contain ,");
}
List<String[]> finalList = new ArrayList<String[]>();
finalList.add(splitInput);
#1
1
As you have mentioned each line has words separated with comma. So you can read the lines and then split the lines to get the array for each line. You may want to add these arrays to another collection such as an ArrayList:
正如您所提到的,每一行都有用逗号分隔的单词。因此,您可以读取线条,然后分割线条以获得每条线条的数组。您可能希望将这些数组添加到另一个集合,例如ArrayList:
So I am assuming you have the file reading logic and each line you are getting in a string called line
. You need to put these pieces at the right places in your code:
所以我假设您有文件读取逻辑,并且每行都在一个名为line的字符串中。您需要将这些部分放在代码中的正确位置:
// collection to hold array of words in each line
ArrayList<String[]> listOfWords = new ArrayList<String[]>();
// split your line into an array of words
String wordsInLine[] = line.split("[ ,]+");
// add words array to your collection
listofWords.add(wordsInLine);
#2
1
Things to check out:
要检查的事情:
But particularly, String.split()
:
但特别是String.split():
String list = "first second third";
String[] items = list.split(" "); // = { "first", "second", "third" };
Or with regular expressions:
或者使用正则表达式:
String list = "first, second, third";
String[] items = list.split("[ ,]+"); // = { "first", "second", "third" };
Then you have things like StringTokenizer
, which can do it all, e.g.:
那么你有像StringTokenizer这样的东西,可以做到这一切,例如:
String values = "name, 1, 2, 3";
String name = null;
List<Integer> numbers = new ArrayList<Integer>();
StringTokenizer tokenizer = new StringTokenizer(value, " ,");
if (tokenizer.hasMoreTokens()) // first token is name
name = tokenizer.nextToken();
else
throw new Exception("No name!");
while (tokenizer.hasMoreTokens()) // rest are numbres
numbers.add(Integer.parseInt(tokenizer.nextToken());
Or you can do fun stuff with Scanners:
或者您可以使用扫描仪做有趣的事情:
String values = "name, 1, 2, 3";
Scanner scanner = new Scanner(values);
scanner.useDelimiter("[ ,]+"); // regular expression, 1 or more spaces/commas
String name = null;
List<Integer> numbers = new ArrayList<Integer>();
if (scanner.hasNext()) // first token is name
name = scanner.next();
else
throw new Exception("No name!");
while (scanner.hasNextInt()) // rest are numbers
numbers.add(scanner.nextInt());
There's a lot of ways to skin this cat.
有很多方法可以给这只猫上皮。
#3
0
Arrays are fixed sized, so you would have to know how many elements you want to add via a loop. I would recommend using an ArrayList.
数组是固定大小的,因此您必须知道要通过循环添加多少元素。我建议使用ArrayList。
This should help: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
这应该有所帮助:http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
ArrayList<String> listOfStrings = new ArrayList<String>(); // instantiating the object
listOfStrings.add(STRING_VARIABLE) // adding onto the ArrayList.
listOfStrings.get(INDEX_NUMBER) // gets the actual String object, using the index number
#4
0
String strFromFile = "name,number,number,";
if(strFromFile.contains(","))
{
String[] splitInput = strFromFile.split(",");
String part1 = splitInput[0]; //gives you name
String part2 = splitInput[1]; // gives you number
.....
}
else
{
throw new IllegalArgumentException("String " + strFromFile + " does not contain ,");
}
List<String[]> finalList = new ArrayList<String[]>();
finalList.add(splitInput);