0

I am trying to store the words in a file separated by coma in a java array
The file is

Age,Income,Student,Credit Rating,Class: Buys Computer

Youth,high,No,Fair,No

Youth,high,No,Excellent,No

Middle aged,high,No,Excellent,No

Senior,medium,No,Fair,Yes

Senior,Low,Yes,Fair,Yes

Senior,Low,Yes,Excellent,No

public class Test {
  public static void main(String args[]) throws FileNotFoundException, IOException{ 
    FileInputStream f=new FileInputStream("F:\\pr\\src\\dmexam\\inp2.txt");
    int size,nr=7,nc=5,j=0,i=0;
    char ch;
    String table[][]=new String[nr][nc];
    size=f.available();
    table[0][0]=new String();
    while(size--!=0){
         ch=(char)f.read();
         if(ch=='\n')
         {
             i++;
             if(i>=nr)
                 break;
             table[i][0]=new String();
             j=0;
             continue;
             
         }
         if(ch==',')
         {     
            j++;
            table[i][j]=new String();
            continue;
         }
          table[i][j]+=ch;
    }
    f.close();
    System.out.println("The given table is:::---");
    for(i=0;i<nr;i++){
       for(j=0;j<nc;j++){ 
         System.out.print("  "+table[i][j]);
         System.out.print("  ");
       }
     }
 }  
}

But the output is

The given table is:::---

But if the for is changed like this

System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
    for(j=0;j<nc-1;j++){ 
        System.out.print("  "+table[i][j]);

        System.out.print("  ");
    }
    System.out.println(table[i][nc-1]);
 }

The output is

The given table is:::--- Age Income Student Credit Rating Class: Buys Computer

Youth high No Fair No

Youth high No Excellent No

Middle aged high No Excellent No

Senior medium No Fair Yes

Senior Low Yes Fair Yes

Senior Low Yes Excellent No

I want to know "why System.out.print is not workig???"...

Community
  • 1
  • 1
nicky97
  • 13
  • 3
  • I'm gonna stop you right there and refer you to [Stringbuilder](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) – Mad Matts Aug 23 '16 at 20:08
  • Why do you want to make your life harder and read text as single character (especially with Streams which are supposed to handle data as bytes, not as text since single character doesn't need to be written in single byte)? We have Readers to handle reading data as text. With `BufferedReader` you can read lines as Strings with little help of `readLine()` method. There is also `Scanner` which provides methods like `nextLine()` `nextInt` and so on for other types (just be aware of: http://stackoverflow.com/q/13102045). – Pshemo Aug 23 '16 at 20:23

2 Answers2

8

The PrintStream that System.out uses has an internal buffer, since writing to stdout is relatively expensive -- you wouldn't necessarily want to do it for each character. That buffer is automatically flushed when you write a newline, which is why println causes the text to appear. Without that newline, your string just sits in the buffer, waiting to get flushed.

You can force a manual flush by invoking System.out.flush().

yshavit
  • 39,951
  • 7
  • 75
  • 114
  • I added System.out.flush() after `System.out.print(" "+table[i][j]);` but it is still not working can u tell where should I add it? @yshavit – nicky97 Aug 26 '16 at 13:48
  • Hm, it works for me. What's not working? Is it not showing up any text, or something else? Also, how are you running this -- what OS version of Java, how are you actually running the executable? – yshavit Aug 28 '16 at 23:41
  • It is giving the previous output...I am using netbeans 8.1 and jdk 1.8 in windows 10 @yshavit – nicky97 Sep 19 '16 at 13:58
0

Okay let me try to help you out here. So you are making your life really rough at the moment. Have you tried to look at different libraries like BufferedWritter/FileWritter?

You can easily import these into your project using:

import java.io.BufferedWritter;
import java.io.FileWritter;

It is also recommended to catch errors using the IOException library:

import java.io.IOException;

As for the separation of the words, these libraries give you tools like control over the delimiter. For example we can do something like this:

//this is if you are creating a new file, if not, you want true to append to an existing file
BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt", boolean false));
try
{
    // write the text string to the file
    bw.write("Youth,high,No,Fair,No");

    // creates a newline in the file
    bw.newLine();
}

// handle exceptions
catch (IOException exc)
{
    exc.printStackTrace();
}

// remember to close the file at the end
    bw.close();

Now that is for hard coding the data, but we can do this with a for loop. We can add delimiters in the function within the for loop, for example: (I am not sure how you have the data stored, but I am assuming you save it in an array. I am also assuming there will ALWAYS be 5 sets of data per line)

BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt", boolean false));
for (int i = 1, i <= listName.size()+1, i++) {
    if (i % 5 == 0) {
        bw.write(listName.get(i-1));
        bw.write(", ");
        bw.newLine();
    } else {
        bw.write(listName.get(i-1));
        bw.write(", ");
    }
}

This would write to the file:

Youth,high,No,Fair,No

Youth,high,No,Excellent,No

Middle aged,high,No,Excellent,No

Senior,medium,No,Fair,Yes

Senior,Low,Yes,Fair,Yes

Senior,Low,Yes,Excellent,No

This may make your life a little easier (if I am understanding your needs clearly). Next time please make sure to flesh out your question more than you did.

DISCLAIMER: I did not test all of the code, so if you find an error please let me know and I will edit it as needed. If I get time I will make sure it works.

Daniel Anner
  • 110
  • 10