ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
----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.
----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.
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);
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.
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
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!
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.