Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader.read  RSS feed

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am having trouble executing the following program.


import java.io.*;

class FileTest

{

public static void main (String[] args)
{
FileTest t = new FileTest();
t.readMyFile();
}


void readMyFile()
{
int [] record = new int[9];

try {

FileReader fr = new FileReader("c:\\a.txt");
BufferedReader br = new BufferedReader(fr);


for(int i=0;i<9;i++)
{

record[i] = br.read(int[]);
i++;
System.out.println(record[0]);
}

}

catch (IOException e) {
System.out.println("Uh oh, got an IOException error!");
}

}

}

Basically, I am trying to read each number from a textfile "a" which has 123456789 writen in it into an Integer array. When I executed the program, it is compiling properly but when I run it, instead of getting the output as 1, I am getting
49
49
49
49
49

Could some one please tell me why?/
 
Henry Wong
author
Sheriff
Posts: 22818
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, I am trying to read each number from a textfile "a" which has 123456789 writen in it into an Integer array. When I executed the program, it is compiling properly but when I run it, instead of getting the output as 1, I am getting
49


Unfortunately, the code you provided doesn't compile, so I can only guess that you are calling the read() method that doesn't take any arguments.

Anyway, FYI... 49 is the ASCII code for the character "1".

Henry
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually instead of record[i] = br.read(int[]);

If I give record[i] = br.read(); it is compiling.


The question I have is, Can I read from the textfile which has 1234567 each by single character like first 1, then 2, then so on.. into an Integer Array?/

Thanks.
 
Henry Wong
author
Sheriff
Posts: 22818
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question I have is, Can I read from the textfile which has 1234567 each by single character like first 1, then 2, then so on.. into an Integer Array?/


Sure. In fact, your program is almost done. You just need to convert the ascii characters values to their integer values.

Henry
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you're printing record[0] each time. You probably meant record[i].
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably using readLine() method of BufferedReader will help you more here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!