0

I'm reading from a csv file which has this layout:

CS4092,B
CS2013,A
CS8291,C
CS0927,D
CS0281,A

When I do print it out, I get this output:

CS4092
BMA4042
CCS4023
ACS4075
BCS4010
A

The output I'm trying to achieve is:

CS4092 - B
CS2013 - A
CS8291 - C
CS0927 - D
CS0281 - A

Can someone please tell me what way I can get the desired output. Thanks.

public class Student
{
    private String studentID;

public Student()
{

}
    public void setID(String studentID)
    {
        this.studentID = studentID;
    }
public void checkResults() throws IOException 
{
    String lines = "", unparsedFile = "", myArray[];
    String path = System.getProperty("user.home") + 
                             "/NetBeansProjects/CS4013Project/src/" +
                             this.studentID + "Results.csv";
    FileReader fr = new FileReader(path);
    try (BufferedReader br = new BufferedReader(fr)) 
    {
        while((lines = br.readLine()) != null)
        {
            unparsedFile += lines;
        }
    }
   myArray = unparsedFile.split(",");

   for(String item : myArray)
   {
       System.out.println(item);
  }
 }
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
ASmoothNoble
  • 39
  • 1
  • 5
  • read each line from the file, replace ',' with ' - '. use `replaceAll(String regex,String replacement)` method – Nishant Nov 21 '13 at 18:37

4 Answers4

0

You need to insert a comma between each pair of lines as you concatenate them to form unparsedFile.

Without that, unparsedFile contains:

CS4092,BCS2013,ACS8291,CCS0927,DCS0281,A

You need it to contain:

CS4092,B,CS2013,A,CS8291,C,CS0927,D,CS0281,A

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0
    int i = 0;
    for (String item : myArray)
    {

        System.out.print(item);//It doent add a new line after printing the item
        if (++i % 2 != 0)
        {
            System.out.println(("-")); //if just one printed just append -
        }
        else
        {
            System.out.println(); //else put a new line and reset the flag
            i=0;

        }
    }
Freaky Thommi
  • 756
  • 7
  • 18
0

You are concatenating all the lines together (without newline separators). You should instead create an ArrayList<String> of lines and then split each of those.

Or use a CSV processing library.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0

Try replacing the while loop with:

while ((lines = br.readLine()) != null) {
   String myarray[] = lines.split(",");
   System.out.format("%s - %s\n", myarray[1],myarray[0]);
}

And remove the code after this

Jack burridge
  • 472
  • 7
  • 21
  • Thanks this worked, but lets say I have an array that has 3 elements i.e. CS4075, B, 2013 System.out.format("%s - %s\n", myArray[0],myArray[1], myArray[2]); I used that but I'm only getting the output of myArray[0] and myArray[1]. Can you please help? – ASmoothNoble Nov 21 '13 at 19:23
  • Read up on string formatting http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html. System.out.format takes 1+n arguments, the first is the string i.e. "%s - %s" then rest are what gets placed in the replacement fields, %s for string, %d for decimal and %f for float. Change "%s - %s\n" to "%s - %s - %s\n" and it should work – Jack burridge Nov 22 '13 at 00:47