So I am trying to create a Course Object which has the parameters of
String courseNum, String courseSect, String courseName, String courseGrade, double courseHours, String courseTerm
and sample input in the form of a string is the following
APSC1023 AA01B MECHANICS II B+ 5.00 2009/WI
the course number being APSC1023 couseNum and courseSect being AA01B and Mechanics and so on, the problems come into play with the way the fields are separated. I was thinking that since the only time there is only one space is in the name field (there is only 1 space) that you would use this as your case to not move onto assigning the next variable and do some sort of loop untill there is more then that 1 space. My question is how can you go about ignoring the other cases and only look after this one specific case.
-
1Sounds like you need to do some research into regex and string parsing.tnw– tnw2015年11月17日 21:32:16 +00:00Commented Nov 17, 2015 at 21:32
-
1I would think that you would just read from the input file until you encounter white-space, then, when you no longer encounter white-space, just read into the next variable and so onbpgeck– bpgeck2015年11月17日 21:32:19 +00:00Commented Nov 17, 2015 at 21:32
-
1It looks like your information is column delimited. In other words, the courseNum starts in column 1 and is 8 columns in length. The courseSect starts in column 12 and is 5 columns in length. This looks like a job for Cobol :-)Gilbert Le Blanc– Gilbert Le Blanc2015年11月17日 21:33:12 +00:00Commented Nov 17, 2015 at 21:33
-
might be easier to just use JSON string, and then convert it to an object using jackson or similar.t0mm13b– t0mm13b2015年11月17日 21:40:26 +00:00Commented Nov 17, 2015 at 21:40
1 Answer 1
String str = "APSC1023 AA01B MECHANICS II B+ 5.00 2009/WI";
String[] data = str.split("\\s+");
When data.length == 6, I assume there is no space in the courseName.
When data.length == 7, I assume there is a space in the courseName.
-
would this work with course names with more than 2 parts too it?Thorx99– Thorx992015年11月17日 21:50:58 +00:00Commented Nov 17, 2015 at 21:50
-
If the course name has some number of space and all others are not, data[0], data[1], data[data.length-1], data[data.length-2], data[data.length-3] are mapped to all other fields. String concatenation of the remaining data is for the course name.Jihwan– Jihwan2015年11月17日 22:01:55 +00:00Commented Nov 17, 2015 at 22:01
-
the thing is you could have names like ELECTRICAL & CMPE ENG DESIGN where there is still only 1 space separating the nameThorx99– Thorx992015年11月18日 00:33:00 +00:00Commented Nov 18, 2015 at 0:33