0

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.

asked Nov 17, 2015 at 21:28
4
  • 1
    Sounds like you need to do some research into regex and string parsing. Commented Nov 17, 2015 at 21:32
  • 1
    I 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 on Commented Nov 17, 2015 at 21:32
  • 1
    It 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 :-) Commented 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. Commented Nov 17, 2015 at 21:40

1 Answer 1

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.

answered Nov 17, 2015 at 21:39
3
  • would this work with course names with more than 2 parts too it? Commented 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. Commented 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 name Commented Nov 18, 2015 at 0:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.