0

I'm very new to the world of Java programming, and although I know this is a ridiculously easy question, I can't seem to phrase my searches in a way that turns up the answer I need...so hopefully someone from this community won't mind helping me.

MY program needs to take an input line from a .csv file and split it into fields of an array, using commas as delimiters. The fields of the array are then assigned to variables that are different data types - char, int, float, and string. What I'm struggling with is the formatting for my String variables.

Here is part of my code:

public void parseCSV(String inputLine) {

String[] splitFields;
splitFields = inputLine.split(",");

try {
    empNumber = Integer.parseInt(splitFIelds[0[);
    payType = splitFields[1].charAt(0);
    hourlyRate = Float.parseFloat(splitFields[2]);
    last name =

I need to assign variable lastName, a String data type, to position 3 of my splitFields array. I just don't know how to format it. Help would be greatly appreciated!

3 Answers 3

2

A warning on your overall approach

Go with the other answers if you're doing a homework assignment with a simple csv file, but splitting a String on the comma character , will not work for more complicated CSVs. Example:

"Roberts, John", Chicago

This should be read as two cells where the first string is Roberts, John. Naive splitting on , will read this as three cells: "Roberts, John", and Chicago.

What you should be doing (for robust code)

If you're writing serious/production level code, you should use the Apache Commons CSV library to parse CSVs. There are enough tricky issues with commas and quotations, enough variation in possible formats that it makes sense to use a mature library. There's no reason to reinvent the wheel.

Another tool for parsing text

If you're a beginner, this might be opening up a can of worms, but a powerful tool for parsing/validating text input is "regular expressions." Regular expressions can be used to match a string against a pattern and to extract portions of a string. Once you have extracted a String from a specific cell of a csv, you could use a regular expression to validate that the String is in the format you're expecting.

While you're unlikely to really need regular expressions for this project, I thought I'd mention it.

Sign up to request clarification or add additional context in comments.

1 Comment

I will definitely keep regular expressions and the Apache Commons CSV library in mind for future projects. Being a self-professed noob, I haven't really reached that level yet, but it's good to know. Thank you!
1

String.split(...) returns a String[] so you really can just assign a specific index to a String.

String s = "one two dog!";
String[] sa = s.split(" ");
String ns = sa[1]; // ns now equals "two"

so you can just:

last_name = splitFields[index]; // this will work fine as long as index is within the `array` bounds.

Please mind that your last name var has a space(that might have been you problem).

I also recommend minding the parses, Integer.parseInt(...) & Float.parseFloat(...) might throw a NumberFormatException if you try to parse a non decimal values.

2 Comments

The space was just a typo - it appears as lastName in my program. Thank you for catching that, though. :) I've just been learning about exceptions and I have a try...catch in place to hopefully catch those. Thanks!
you should check out @MatthewGunn answer he has an important point there.
0

Easy, it is already a String, so you do not have to perform additional parsing. The following assignment will do the trick:

lastName = splitFields[3];

1 Comment

I knew it wouldn't be a complicated answer - it was just irritating to not know. That's exactly what I needed. Thank you!

Your Answer

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.