Reading Data from the Text File
I'm trying to read the Data that is present in abc.txt.
The code that I wrote is given below.
import java.io.*;
import java.util.*;
class test
{
publicstaticvoid main(String args[])
{
StringBuffer sb=new StringBuffer();
int rows=0;
int totalvalues=0;
int columns=0;
String s[];
try
{
//scanner is used to scan the textfile
Scanner sc=new Scanner("abc.txt");
//scan each line in the text file
while(sc.hasNextLine())
{
sb.append(sc.nextLine());
rows++;
}
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
s=(sb.toString()).split("");
totalvalues=s.length;
columns=totalvalues/rows;
System.out.println("rows:"+rows+"Columns"+columns);
double normaldata[][]=newdouble[rows+1][columns+1];
int k=0;
System.out.println("S length is:"+s.length);
try{
for(int i = 1; i < rows; i++)
{
for(int j = 1; j < columns; j++)
{
normaldata[i][j] = Double.parseDouble(s[k]);
k++;
}
}
}
catch(Exception e)
{
System.out.println("Exception internally:"+e);
}
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=columns;j++)
{
System.out.print(normaldata[i][j]+"");
}
System.out.println();
}
}
}
[3218 byte] By [
sarath44a] at [2007-11-27 9:11:22]

First of all: indent your code sanely. What you have there is difficult to read.
Why are you concatenating all the lines in the file into one long line, and then splitting it, and then taking the split Strings and turning them back into rows and columns? Wouldn't it be much easier to preserve the existing rows in the file?
Why are you splitting by " "? Are you certain that the data in the file is in exactly that format?
What format is the content of the file in, exactly?
Are you aware that you're creating more rows and columns in the 2d array than you're actually using?
If the file contains data in a fixed-width format, it would probably be easier to parse it using String.substring().
First, when you read in a line, don't append it to a giant StringBuffer.
Split the line you read and store its values immediately. And use a java.util.List rather than an array, so you won't need to know how many rows there are in advance.
Also, rather than splitting on "", split on "\\s+", which splits on any arbitrary amount of whitespace.
Convert the data into the type it represents as soon as you split it, and split it as soon as you read a line in.
What does input represent? Does each line represent a separate record? Does each field have a different meaning?