1

I'm getting weird output when I scan in a csv file split it by "," ,which for the length of each line I append the ith element with i + ":" to the printwriter.My original input looks something like this.

8.035156 7.619141 7.105469

7.234375 7.8125 8.244141

6.615234 8.224609 6.361328

which they are indeed seperated by ",".

The output should look something like this

1:8.035156,2:7.619141,3:7.105469,4:7.072266

again it should be seperated by ",".

but instead the output looks like this, even weirder when i click on the selection it gives me the time.

01:08.0 02:07.6 03:07.1 04:07.1 05:07.4 06:07.2 07:07.6 08:07.1 09:07.1 10:07.2 
12:04:06 AM

The wrong output only happens when I add the , character at the end of the append statement.

public class GeneCsv
{
    public static void main(String[] args) throws IOException
    {
        File file = new File("file.csv");
        FileWriter writer = new FileWriter("/Users/home/fileExpression.csv");
        PrintWriter pw = new PrintWriter(writer);
        Scanner in = new Scanner(file);
        boolean firstLine = true;
        String[] temp = null;
        while (in.hasNextLine())
        {
            if (firstLine == true)
            {
                pw.println(in.nextLine());
                firstLine = false;
                continue;
            }
            else
            {
                String line = in.nextLine();
                temp = line.split(",");
                for (int i = 0; i < temp.length; i++)
                {
                    pw.append(i + ":" + temp[i] + ",");
                }
                pw.append("\n");
            }
        }

        pw.flush();
        pw.close();
        writer.close();
    }
}
Eric Rogers
  • 37
  • 3
  • 7
  • 1
    Show us at least some of the input file. – QuantumMechanic May 23 '12 at 03:35
  • Welcome to SO. When posting code, please try to format it for readability. Doing so will get a lot more people to read it. I've reformatted it for you... this time. – Jim Garrison May 23 '12 at 03:37
  • This was my original input 8.035156 7.619141 7.105469 7.072266 This is what I want my output to look like 1:8.035156 2:7.619141 3:7.105469 4:7.072266 Unforunately this is what I'm getting 01:08.0 02:07.6 03:07.1 04:07.1 I dont understand how my output even comes remotely close to this.And btw i'm using ms excel. – – Eric Rogers May 23 '12 at 03:46
  • @EricRogers Edit your post and add the input there. Does your input contain commas between the numbers? – Jim Garrison May 23 '12 at 03:52
  • Open your output file in a simple editor. (notepad, textpad etc) – HashimR May 23 '12 at 03:56
  • 1
    @Eric - You have not accepted any answer for your prev qusetion where you got solution for getting correct output in first place (http://stackoverflow.com/questions/10695619/getting-weird-time-like-output-for-csv-file/10696939#10696939). SO being collabarative platform for Q & A,it will be good if you can atlest accpet the answer which helped you in reaching up to this point – ejb_guy May 23 '12 at 04:48

1 Answers1

0

There is nothing wrong with your code. The problem is that Excel is interpreting your output as a series of time values. Thus, the value in the first column 1:8.035156 is being read as 1 minute, 8.035156 seconds. If you look at the actual data output you'll see it matches the desired format.

To get Excel to treat the values as text you will need to format them as

="1:8.035156",="2:7.619141",="3:7.105469",="4:7.072266",

in the CSV file. The leading = and surrounding quotes do the trick.

See also Stop Excel from automatically converting certain text values to dates

Community
  • 1
  • 1
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190