Reputation: 3
I'm a student who is learning java (just started this year. I just graduated so I can't ask my high school teacher for help, either), and I need some help on how to turn a string into multiple arrays(?).
However, after this I get stuck (because I can't find a way to break down the string for my purposes).
The file is read like this
Input: The first line will be the number of cases. For each case the first line will be Susan’s work schedule, and the second line will be Jurgen’s work schedule. The work schedules will be the date of the month (1-30) which they work. The third line will be the size of the initial TV.
The file in question:
3 2 5 10 2 4 10 60 2 4 10 2 5 10 60 1 2 3 4 7 8 10 12 14 16 18 20 24 26 28 30 1 2 3 6 9 10 17 19 20 21 22 23 25 27 28 29 20
I'm not sure how to go about this. I've tried .split()
, but that only seems to work on the first row in the string. Any help/tips you might have would be greatly appreciated!
Upvotes: 0
Views: 390
Reputation: 1090
Hey I got a great solution for you.
Just make a class to store each student's data like
import java.util.List;
public class Student {
private int noOfCases;
private List<String> workSchedule;
private List<String> initialTV;
//getter setters
}
Then this...
public static void main(String[] args) {
//3 students for reading 9 lines
//Susan, Jurgen and You ;)
Student[] students = new Student[3];
int linesRead = 0;
String aLine = null;
// read each line through aLine
for (Student student : students) {
//use buffered/scanner classes for reading input(each line) in aLine
while (aLine != null) {
++linesRead;
if (linesRead == 1) {
student.setNoOfCases(Integer.valueOf(aLine));
++linesRead;
} else if (linesRead == 2) {
student.setWorkSchedule(Arrays.asList(aLine.split(" ")));
++linesRead;
} else if (linesRead == 3) {
student.setInitialTV(Arrays.asList(aLine.split(" ")));
} else {
linesRead = 0;
}
}
}
}
}
If you need to read more lines/more student's records, just resize the Student Array!!
Upvotes: 0
Reputation: 774
Depending on how you are reading the file, you are probably using something like the following:
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
Then, to read the file line by line:
String theLine = in.readLine();
Now you have the string corresponding to the line (in this case the first line, but you can call readLine()
over and over to read them all), but note you do not read the 'whole' file at once to get all the Strings, as I think you are suggesting, rather the reader takes a line at a time.
Therefore, if you want all the lines held in an array, all you need to do is declare the array, and then read each line into it. Something like:
// Declare an array to put the lines into
int numberOfLines = 10;
String[] arrayOfStrings = new String[numberOfLines];
// Read the first line
String aLine = in.readLine();
int i = 0;
// If the line that has been read is null, it's reached the end of the file
// so stop reading
while(aLine != null){
arrayOfStrings[i] = aLine;
i++;
aLine = in.readLine();
}
// arrayOfStrings elements are now each line as a single String!
Of course, you may not know how many lines are in the file to be read in the first case, so declaring the size of the array is difficult. You could then look into a dynamically-scaling data structure such as ArrayList
, but that is a separate matter.
Upvotes: 1
Reputation: 35453
Given you already have your BufferedReader
, you could try something like this to give you a multi dimensional array of your rows and columns:
BufferedReader reader = ...;
List<String[]> lines = new ArrayList<String[]>();
String input;
while((input = reader.readLine()) != null){
if (!input.trim().isEmpty()){
lines.add(input.split(" "));
}
}
String[][] data = lines.toArray(new String[lines.size()][]);
Upvotes: 0
Reputation: 486
you may read your input by lines and then apply regex on them to separate numbers from string like:
String numbers = "1 2 3 4 7 8 10 12 14 16 18 20 24 26 28 30";
String[] singleNumbers = numbers.split(" ");
then you will have these numbers separated by space in singleNumbers array
Upvotes: 1