0

This is what it does:

2,
4,
6,
8,
10

I would like for it to be horizontal: 2, 4, 6, 8, 10

        try    { 
            PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("numbers.dat"))); 
            for(int i = start; i <= 100; i = i + 2)    { 
                fout.println(i+","); 
            } 
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Mike
  • 2,293
  • 13
  • 42
  • 56
  • possible duplicate of [Clearest way to comma-delimit a list (Java)?](http://stackoverflow.com/questions/668952/clearest-way-to-comma-delimit-a-list-java) – wkl Mar 28 '11 at 16:31
  • 1
    println prints the string passed to it and then a newline character. So if you want to print all the data on the dame line, then use print rather than println which is same as println, but doesn't insert a newline character after the message. – Logan Mar 28 '11 at 17:47
  • @Logan: That worked, is there a way to remove the last comma. It ends like this: 100, – Mike Mar 28 '11 at 17:50

5 Answers5

2

Another attempt

for(int i = start; i <= 100; i += 2) 
    System.out.print(i + (i > 98 ? "\n" : ", "));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Here is a very basic example of what you could do:

int max=11;
for(int i = 0; i < max; i += 2){
    System.out.print(i);
    if(i < max-2){
        System.out.print(", ");
    }
}
Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • I think you meant `i = start` and if `(i < max-2)` – MByD Mar 28 '11 at 16:35
  • @MByD: For the purpose of my example `i` starts at `0`. And, since we are incrementing by `2`, I agree with your second amendment. – Jeremy Mar 28 '11 at 16:49
1

In the write() method:

 for(int i = start; i <= 100; i = i + 2)    { 
    if (i > start) {
        fout.print(",");
    }

    fout.println(i); 
}

Then when you call output(), it will display as comma separated list. And for screen display

while((outputline = fin.readLine()) != null) { 
    System.out.print(outputline + " "); 
}
System.out.println();

Alternatively, skip saving the comma in the file and displaying will be as follows

int count = 0;
while((outputline = fin.readLine()) != null) { 
    if (count > 0)
        System.out.print(", ");

    System.out.print(outputline); 
    count++;
}
System.out.println();
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
1

If you mean to do it on printing to your .dat file:

 for(int i = start; i <= 100; i = i + 2) { 
     fout.print(i);
     if(i < 100) {
         fout.print(", ");
     } else {
         fout.println();
     }
  } 

If it is when printing to the system output after reading your file, try this:

      try    { 
        BufferedReader fin = new BufferedReader(new FileReader("numbers.dat")); 

            String outputline = ""; 
            StringBuilder result = new StringBuilder();
            while((outputline = fin.readLine()) != null)    { 
            result.append(outputline).append(", "); 
            } 
            int length = result.length();
            result.delete(length - 2, length);
            System.out.println(result);
            fin.close(); 
        } 

This uses a StringBuilder to build up your result and then removes the last , when it is done.

justkt
  • 14,610
  • 8
  • 42
  • 62
0
fout.println(i + ", ");  

System.out.println(outputline + ", ");  

This will give you commas after all the numbers but it looks like you don't need one after the final number.

You will have to remove the last comma either by using indexOf or checking for the last iteration of your loop.

blong824
  • 3,920
  • 14
  • 54
  • 75
  • You don't have a clue on the file format of `numbers.dat`, what makes you sure that your code for file reading & displaying will work? – Buhake Sindi Mar 28 '11 at 16:35