java - System.out.println prints the string but System.out.print does not -
i trying store words in file separated coma in java array
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 output is
the given table is:::---
but if changed 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 know "why system.out.print not workig???"...
the printstream system.out
uses has internal buffer, since writing stdout relatively expensive -- wouldn't want each character. buffer automatically flushed when write newline, why println
causes text appear. without newline, string sits in buffer, waiting flushed.
you can force manual flush invoking system.out.flush()
.
Comments
Post a Comment