Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BufferedReader skill problem

 
Hugo Leung
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I would like to know how to read the white space on the BufferedReader function. For example , i have the abc.txt file the format is :

1 0 0 1 0 1 0
0 1 0 0 0 0 0



But i only can read this format:

1001010
0100000


Some of the below program :

....
...
....
public void aCreate(String file)
{
String inStr ="";
int i =0 , j =0 , index =0;;
int sizeOfa;
try
{
BufferedReader ioReader = new BufferedReader(new FileReader(file));
sizeOfa=Integer.parseInt(ioReader.readLine());
a = new int[sizeOfa][sizeOfa];
while ((inStr = ioReader.readLine())!= null)
{
for (j =0; j < inStr.length();j++)
{
System.out.print((a[i][j] = Integer.parseInt(new Character(inStr.charAt(j)).toString())));
}
i = i +1;
System.out.print("\n");
}
}
catch(Exception e)
{
e.printStackTrace();
}


....
...
....
System.out.println("The Path is:");
}


In fact, i would to read have white space txt file . thanks you
 
Kamal Joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't get your code actually. But anyways if you just want to read each character
every invocation of readline() returns a String object.( which consists of all the spaces in the line )

From this String either you can retrieve a char array (inStr.toCharArray()) then happily index any character
or retrieve each character directly using (inStr.charAt(index))


/*sample code*/

public void aCreate(String file) {
String inStr = "";

try {
BufferedReader ioReader = new BufferedReader(new FileReader(file));
while ((inStr = ioReader.readLine()) != null) {
for(int x=0;x<inStr.length();x++){
System.out.print(inStr.charAt(x));
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}


}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic