LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2008, 12:38 AM   #1
joegumbo
Member
 
Registered: Sep 2006
Distribution: MX-16 Modified using TDE
Posts: 239

Rep: Reputation: 32
Java - reading from a file into an array (SOLVED!)


Hi,

This is part of an assignment, so I would like advice on this without actually solving this for me. Thanks.

I'm trying to read from two text files into an array:

Scanner billsOf2006Scanner = new Scanner(new File("billsOf2006.txt"));
for (int k = 0; k < 12; k++)
{
year2006[k]=billsOf2006Scanner.nextDouble();
}


Even when I include the full path, I still keep getting the same error messages:

Exception in thread "main" java.io.FileNotFoundException: billsOf2006.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.util.Scanner.<init>(Scanner.java:636)
at TheArrayProgram.main(TheArrayProgram.java:47)


------------------
(program exited with code: 1)
Press return to continue




I've tried /home/username/TheArrayProgram.java/billsOf2006.txt

as well as appending first file:///home/blahblah and File:///home/blahblah

My text file is in the same directory as the program, but the program cannot find it.

Btw, this is on Slackware 12.1.

Thanks,
-Joe G.

Btw, upon further viewing, I get the impression that this could be more of a Linux question, rather than a Java question. If I remember correctly, all that should be necessary in Windows is for the text files to be included in the same folder as the program.java file. But, maybe I'm wrong?

Thanks,
-Joe

Last edited by joegumbo; 09-29-2008 at 02:40 PM.
 
Old 09-29-2008, 01:23 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

It *should* work OK... but there are a couple of potential "gotcha's" here:

1. Linux is case-sensitive: if the file isn't spelled *exactly* as you specified in your program, it will fail

2. If you're using Java packages, you might not be in the directory you expect to be in

3. Etc etc

SUGGESTION:
Try - and adapt - this code snippet to further debug the problem:
Code:
import java.io.*;

public class Test
{
  public static void main (String[] args)
  {
     String fname = "billsOf2006.txt";
     File f = new File (fname);
     if (f.exists ())
       System.out.println ("SUCCESS: " + fname + " exists!");
     else
       System.out.println ("WARNING: " + fname + " doesn't exist!");
  }
}
'Hope that helps .. PSM
 
Old 09-29-2008, 03:56 AM   #3
linuxer8786
Member
 
Registered: Sep 2008
Posts: 43

Rep: Reputation: 15
you missing FileInpuStream and FileReader

Linux Archive

Last edited by linuxer8786; 10-12-2008 at 01:29 AM.
 
Old 09-29-2008, 08:19 AM   #4
joegumbo
Member
 
Registered: Sep 2006
Distribution: MX-16 Modified using TDE
Posts: 239

Original Poster
Rep: Reputation: 32
This is from the compiler:


----jGRASP exec: javac -g /home/joegumbo/Documents/CIT-130-NC61W/Test/Test.java
Test.java:15: cannot find symbol
symbol : variable billsOf2006
location: class Test
File f = new File (billsOf2006.txt);
^
Test.java:17: cannot find symbol
symbol : variable billsOf2006
location: class Test
System.out.println ("SUCCESS: " + billsOf2006.txt + " exists!");
^
Test.java:19: cannot find symbol
symbol : variable billsOf2006
location: class Test
System.out.println ("WARNING: " + billsOf2006.txt + " doesn't exist!");
^
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.


This is the program output:


----jGRASP exec: java Test
WARNING: billsOf2006.txt doesn't exist!

----jGRASP: operation complete.


I've checked and rechecked the spelling many times.

Thank you paulsm4!
-Joe

Hi linuxer8786!
I have to get out of the house and go to work. I'll check into those libraries later today when I get home.

These are my import statements:

import java.io.*;
import static java.lang.System.*;
import java.util.*;
import java.util.Scanner;
import java.io.File;
import java.io.File.*;
import java.io.IOException;


If those libraries aren't there, then where are they?

Thanks,
-Joe

Last edited by joegumbo; 09-29-2008 at 08:32 AM.
 
Old 09-29-2008, 09:24 AM   #5
linuxer8786
Member
 
Registered: Sep 2008
Posts: 43

Rep: Reputation: 15
Quote:
Originally Posted by joegumbo View Post
This is from the compiler:


----jGRASP exec: javac -g /home/joegumbo/Documents/CIT-130-NC61W/Test/Test.java
Test.java:15: cannot find symbol
symbol : variable billsOf2006
location: class Test
File f = new File (billsOf2006.txt);
^
Test.java:17: cannot find symbol
symbol : variable billsOf2006
location: class Test
System.out.println ("SUCCESS: " + billsOf2006.txt + " exists!");
^
Test.java:19: cannot find symbol
symbol : variable billsOf2006
location: class Test
System.out.println ("WARNING: " + billsOf2006.txt + " doesn't exist!");
^
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.


This is the program output:


----jGRASP exec: java Test
WARNING: billsOf2006.txt doesn't exist!

----jGRASP: operation complete.


I've checked and rechecked the spelling many times.

Thank you paulsm4!
-Joe

Hi linuxer8786!
I have to get out of the house and go to work. I'll check into those libraries later today when I get home.

These are my import statements:

import java.io.*;
import static java.lang.System.*;
import java.util.*;
import java.util.Scanner;
import java.io.File;
import java.io.File.*;
import java.io.IOException;


If those libraries aren't there, then where are they?

Thanks,
-Joe
It in java.io
you try something:
File file = new File("abc");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buf = new BufferedInputStream(fis);
 
Old 09-29-2008, 11:24 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
joegumbo -

Please ignore linuxer8786 for the time being. You're going to need "FileInputStream()" et al (or something like it) soon, but it's not the first problem we need to resolve. I deliberately wanted you to focus on the "File" object, for two reasons:
a) We need to "get it right" before we go any further
b) Understanding the distinction between "File", "Stream" and "Scanner" classes is *extremely* important (and very useful!)

Also, you've got way too many imports. They're not "bad" .. but they're not required, either.

Please cut/paste this version of "Test.java" exactly as-is, and try to build it:
Code:
import java.io.*;

public class Test
{
  public static void main (String[] args)
  {
     // Check whether or not this file exists in this directory
     String fname = "billsOf2006.txt";
     File f = new File (fname);
     if (f.exists ())
       System.out.println ("SUCCESS: " + fname + " exists!");
     else
       System.out.println ("WARNING: " + fname + " doesn't exist!");
     /*
      * NOTE: 
      * A Java "File" object is just something on a filesystem - it's *not* 
      * necessarily a "text file", and it doesn't necessarily even have to exist!
      */

     // Print out the working directory
     System.out.println ("user.dir= " + System.getProperty("user.dir") + "...");

     // List all files in this directory
     File dir = new File(".");
     String[] children = dir.list();
     if (children != null) 
     {
       System.out.println ("#/files and subdirectories= " + children.length + "...");
       for (int i=0; i < children.length; i++) 
         System.out.println ("  " + children[i] + "...");
     }
     System.out.println ("Done.");
  }
}
Here's sample output:
Quote:
javac -g Test.java
java Test

WARNING: billsOf2006.txt doesn't exist!
user.dir= C:\temp\temp2...
#/files and subdirectories= 2...
Test.class...
Test.java...
Done.
You should *not* get any warnings or errors.
 
Old 09-29-2008, 02:26 PM   #7
joegumbo
Member
 
Registered: Sep 2006
Distribution: MX-16 Modified using TDE
Posts: 239

Original Poster
Rep: Reputation: 32
Smile

It compiled and output!!

Thank you!

I know you asked me not to make any changes, but sometimes a blank slate is easier.

I made a new directory named TestA;
I changed the program name to TestA.java;
I changed the text fnames to a.txt and b.txt;
I saved a.txt , b.txt and TestA.java in TestA
I enclosed fname in double quotes wherever I found it in TestA.java

It compiled error free.

This is the output from the program:

SUCCESS: a.txt exists!
user.dir= /home/joegumbo/Documents/TestA...
#/files and subdirectories= 5...
a.txt...
b.txt...
TestA.java...
geany_run_script.sh...
TestA.class...
Done.


------------------
(program exited with code: 0)
Press return to continue



paulsm4... You are a genius!

Last edited by joegumbo; 09-29-2008 at 02:31 PM.
 
Old 09-29-2008, 02:46 PM   #8
joegumbo
Member
 
Registered: Sep 2006
Distribution: MX-16 Modified using TDE
Posts: 239

Original Poster
Rep: Reputation: 32
I went back into my original program and changed the text file names to a.txt and b.txt . I'm only guessing, but it seems that Java just doesn't like numbers in a text file name it's reading from.


It's compiling and spitting out the data I'm requesting!

Thank you again, paulsm4!

Thank you too linuxer8786, for trying to help!

Last edited by joegumbo; 09-29-2008 at 02:47 PM.
 
Old 09-29-2008, 03:54 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

In problems like this, you've just got to break things down, a step at a time.

It sounds like you've got a handle on the "file not found" step - cool!

The next step is to do exactly what linuxer8786 suggested:

a) use the file to open a "stream"
b) pass the stream as input to your "Scanner" class

Here's an example:
http://java.sun.com/docs/books/tutor.../scanning.html

'Hope that helps .. PSM

PS:
Java has no problems with digits in filenames. But there are lots of other problems you might run into. I mentioned "case sensitivity" above. Another pop favorite is "spaces in the file name".

Anyway - it sounds like you're on the right track. Good luck!

Last edited by paulsm4; 09-29-2008 at 03:57 PM.
 
Old 09-29-2008, 05:12 PM   #10
joegumbo
Member
 
Registered: Sep 2006
Distribution: MX-16 Modified using TDE
Posts: 239

Original Poster
Rep: Reputation: 32
Actually, I'm done!!!!

I didn't need to do anything else to enter the filenames into the array.

I'm just now printing out my results for class tomorrow!!

Thank you!!!!!

-Joe
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
Java: Reading characters from a text file chief_officer Programming 5 03-26-2007 07:04 PM
Java: Reading from a text file slow on Tomcat robbbert Programming 7 08-25-2006 12:07 PM
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration