Problem related file handling
I have stored nos as 1 2 3 4 5 6 7 8 9 in textfile sort.txt. now from java i want to find sum of these nos I did coding as follows but not getting output
try
{
int s = 0:
FileInputStream fis = null;
fis = new FileInputStream("F: sort.txt");
int m = fis.available();
byte a[ ] = new byte[ m ];
for(int i = 0 ; i < m ; i++)
{
a = fis.read();
s = s + a[ i ];
}
System.out.println("sum = "+ s);
}
catch( )
Better to use the java.util.Scanner class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.htmlThere's even an example how to read numbers from a file in the link above.
If you are using Java 1.5 or later, you could try java.util.Scanner instead of a raw FileInputStream for reading. It provides you with number-scanning methods, i. e. nextInt() will parse the next token in the file as an int.
FileInputStream.read() returns the binary values of the data in the file, i. e. the ASCII code values of the characters '1', blank, '2', blank, ... as you stored them in the file.
The Scanner will, instead, interpret the file as ASCII-encoded. I think, that's what you wanted.
So if you can't use the Scanner class, do not use that FileInputStream directly on your file for reasons mentioned by ProggerFromKupfer. And what would happen if your number file contains number larger than 9 (consists of more than one digit)?
So again, if you can't use the Scanner class, don't use the FileInputStream, but use a BufferedReader instead. Read the file line by line and split each line into an array of String's which you need to convert to numbers again. Here's a little demo how to use a BufferedReader:import java.io.*;
public class Main {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("Main.java");
BufferedReader fileBuffer = new BufferedReader(reader);
String line;
while((line = fileBuffer.readLine()) != null) {
System.out.println("> "+line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Have a look at the String API docs to see how to split a String:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
And the Integer class has a method to parse a String into an int:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html
Good luck.
> I have stored nos as 1 2 3 4 5 6 7 8 9 in textfile
> sort.txt. now from java i want to find sum of these
> nos I did coding as follows but not getting output
.....
I saw your codes and your problem is similar to mine but i already finalized my codes though I'm not sure if this is the one you needed. I just hope this will help too.
import java.io.*;
class numbers
{
public static void main (String args[]) throws IOException
{
FileReader fr = new FileReader("numbers.txt");
BufferedReader br = new BufferedReader(fr);
String line;
String number = "";
int sum = 0 ;
while ( (line = br.readLine()) != null )
{
java.util.StringTokenizer st = new java.util.StringTokenizer (line,"\t");
while (st.hasMoreTokens()){
number = st.nextToken();
sum = sum + Integer.parseInt(number);
System.out.println("Number:" +number);
}
if (!st.hasMoreTokens())System.out.println("Sum:" +sum);
}
}
}
jo01a at 2007-7-9 6:27:03 >
