Solved

reading files and init and main

Posted on 1998-07-22
13
254 Views
Last Modified: 2010-03-30
I have set up an applet which has arrays that are initialised at the start of it however I want to change it to an application s.t. I can read in data from the files, so I included a main statement.. do I put all the data from the init into the body of the main, as when I run it the main is never dealt with...also I wish the data to be read from files into 2-d arrays of type double of unknown size do I use a data input stream...I'm stuck....Any help is most appreciated...TA ALAN (I feel stupid,Ireland)
0
Comment
Question by:jimboy
  • 7
  • 5
13 Comments
 
LVL 1

Expert Comment

by:dryang
ID: 1227748
kindly break up the question into small chunks, please?
0
 
LVL 16

Accepted Solution

by:
imladris earned 70 total points
ID: 1227749
Yes, moving the code from init to main would probably be the
reasonable way to proceed. I can't comment with certainty since
people can code things dozens of different ways, some of which
are rather unexpected. However, again, yes, in general, moving it
from init to main should work fine.

Reading an unknown amount of double data from a file has a couple
of points associated with it. Firstly, the DataInputStream class
you refer to, with its associated method readDouble, will only
be guaranteed to work, if the data was produced by a Java program
using writeDouble (from DataOutputStream) to create it. If it is
coming in from the console, or the file is in ASCII format or
some other variation, other methods will be required.
The amount of data being unknown has no straightforward elegant
solution. One way is to read through the file once to find out
how much data it contains, then allocate an appropriate array,
then read the data in. Or you can use a Vector of Double's.
Vectors "expand" automatically as needed. Or, thirdly, if you
don't like the extra malarky associate with accessing Vectors,
you can "expand" an array of doubles yourself as needed sort of
like so:

double dblarray[]=new double[50];
. set up DataInputStread di for the sake of argument....
int i=0;
do
{   dblarray[i++]=di.readDouble();
    if(i==dblarray.length)
    {    double tmp[]=dblarray;
         dblarray[]=new double[tmp.length+50];
         System.arraycopy(tmp,0,dblarray,0,tmp.length);
    }
} while(dblarray[i-1]!=-1);

This above is demonstrate the array "expanding" principle, not
the reading with readDouble, which you may not use, and which,
I think, actually throws an IOException you will have to handle,
and would probably use for actual end-of-file detection, rather
than the -1 end value I hypothesized sloppily up there.

0
 

Author Comment

by:jimboy
ID: 1227750
The file is in ascii format unfortunately..what to do???
0
 
LVL 16

Expert Comment

by:imladris
ID: 1227751
Well, that will depend on the form of the data. At the simple end of the scale you use the readLine() method of the DataInput, which will give a line of the file in a string. Assuming that contains a single number you then call Integer.parseInt(String l) (or some appropriate equivalent from Float or Double or whatever) and it will hand you the number.

On the other end of the scale you can attach a StreamTokenizer to the inputstream then call its nextToken method, and as long as it reports that the next token is a number (TT_NUMBER) you can find it in nval (I think).

Check up on the docs for whichever one you think you need. If you still have questions, I will need more detail on the form of the input file.

0
 

Author Comment

by:jimboy
ID: 1227752
public void readfile() throws IOException
    try {
      fin=new FileInputStream(new File ("C:/WINDOWS/Desktop/Aljava/Project2/airp.dat"));
   }
    catch (FileNotFoundException e){
//****print an error if the file is not available****//
    text[1].setText("File not found");
    return;
  }
   dat = new StreamTokenizer(new FileReader ("C:/WINDOWS/Desktop/Aljava/Project2/airp.dat"));
      while(dat.nextToken()!=dat.TT_EOF) {
            airparr = dat.nextToken();
      }
}
imladris this is my code for this example, every time i run it the value returned is 3 even tough the value I wish is 23.47...I'm lost, I think I need a holiday your help is most appreciated as although it doesn't look like it I think the solution is close at hand! Help me please...airparr is a double value, it will eventually be an array but if I could just get one value being read in properly I would be YEAH happy.


TA
0
 
LVL 16

Expert Comment

by:imladris
ID: 1227753
Your almost there. I believe you are in fact getting a negative three returned from nextToken, which is the constant it uses to indicate that the token it found was a number (StreamTokenizer.TT_NUMBER). To get the actual number you can access the instance variable nval. So you get

while(dat.nextToken()!=dat.TT_EOF){
if(dat.nextToken()==dat.TT_NUMBER)airparr=dat.nval;
}


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jimboy
ID: 1227754
imladris I AM NOT WORTHY.......
0
 

Author Comment

by:jimboy
ID: 1227755
while(dat.nextToken()!=dat.TT_EOF){
                  if(dat.nextToken()==dat.TT_NUMBER)
                        airparr=dat.nval;             
            }
imladris i'm back I don't know what is going wrong now I get a value of 4.0 for airparr my file is saved as a w.p 6.1 file with only 1 value in it.....peeved with JAVA
0
 

Author Comment

by:jimboy
ID: 1227756
it's 3.0 again......ooh!
0
 
LVL 16

Expert Comment

by:imladris
ID: 1227757
W.P 6.1 file? Do you mean a wordperfect file? If so, I wouldn't
use that. Word processors stick all kinds of gnarly stuff in
their files, about fonts and sizes and headers and footers etc.
etc. etc.

Use notepad, or some other simpleminded editor.

0
 

Author Comment

by:jimboy
ID: 1227758
when i use notepad it reads it in as 0.0????????????Are you as peeved with this as I am? Ta for all your help AGAIN
0
 

Author Comment

by:jimboy
ID: 1227759
One final question if you don't mind and i'll be out of your hair for ever (phew i hear you sigh)....if there are other values in the file eg -#b how do i get my function to skip these values???????
0
 
LVL 16

Expert Comment

by:imladris
ID: 1227760
Should magically happen, since anything with characters in it will return with a token type of TT_WORD, and so the if in the loop will fail and airpar will not be assigned.

W.r.t. the values you're getting, perhaps the next thing to do is check what Java is seeing, to make sure that you and Java agree on its contents. Make a small file, and output it to the console with a small loop like:

DataInputStream di=new DataInputStream(new
FileInputStream(filename));
String line;
boolean done=false;
do
{  try
   {   line=di.readLine();
       System.out.println(line);
   } catch(IOException e)
   {   done=true;
   }
} while(!done);

This will allow you to see what Java thinks is in the file.

Let me know what you find.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now