Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The system can only find one of the two files specified

 
Claire Saunders
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

I'm in a programming class and we're writing our first program with scanners. I'm using two different text files. Unfortunately, only one of them works-- the other is consistently giving me this error message:

Exception in thread "main" java.io.FileNotFoundException: AccInfo.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at comp401pkg.AnalyzeAccFootball.readTeamInfo(AnalyzeAccFootball.java:18)
at comp401pkg.AnalyzeAccFootball.main(AnalyzeAccFootball.java:12)

The file is saved in the same location as the java and the working text file, though, so it should work fine. Or so I thought. Any suggestions as to what could be wrong would be greatly appreciated!

Full code, in case you need it:
package comp401pkg;
import static java.lang.System.out;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class AnalyzeAccFootball {
private static AccFootballTeam[] team = new AccFootballTeam[12];

public static void main(String[] args) throws FileNotFoundException {
readTeamInfo();
printTeams();
}


public static void readTeamInfo() throws FileNotFoundException {
Scanner input = new Scanner(new File("AccInfo.txt"));
for (int i = 0; i < 12; i++) {
String line = input.nextLine();
String[] word = line.split(";");
team[i] = new AccFootballTeam(word[0], word[1], word[2] );
}
}


public static void readWeekInfo() throws FileNotFoundException {
Scanner input = new Scanner(new File("Week1.txt"));
for (int i = 0; i < 12; i++) {
String line = input.nextLine();
String[] word = line.split(";");
for (int k = 0; k < 12; k++) {
if (team[k].abbreviation==word[1]) {
team[k].score = word[2];
team[k].opponentScore = word[3];
}
else
if (team[k].abbreviation==word[4]) {
team[k].score = word[3];
team[k].opponentScore = word[2];
}
}
}
}


public static void printTeams() {
for( int i = 0; i < 12; i++ ) {
out.println(team[i].toString() );
}
}
}
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the permissions on the file - FileNotFoundException is also thrown if the application doesn't have access rights to a file.
 
Claire Saunders
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Check the permissions on the file - FileNotFoundException is also thrown if the application doesn't have access rights to a file.

I've tried swapping the files, just to check-- AccInfo.txt gives me no error messages in the second scanner, and Week1.txt gives the same error message in the first. So I do have access rights to it. Thanks for the suggestion, though!


EDIT-- I realized why the second file wasn't giving me any error messages. I had no "readWeekInfo()" under main. Now both give me the error message.

So how do I check if I have access rights to the file?
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put the files in class path location and see whether it works?
 
Claire Saunders
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul.p Kumar wrote:put the files in class path location and see whether it works?

=D That worked! At last!

Of course now I have to deal with all the other errors that I haven't noticed 'til now. Such is life. :P
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java files are not used by jvm to run the main class, these are compiled class files which are used and so all path should be with reference to class files.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put the files in class path location and see whether it works?

This sounds wrong. Files are not loaded from the classpath, so if this made a difference then something else is likely not right.

team[k].abbreviation==word[1]

Strings are not compared using the "==" operator, they're compared using the "equals" method. This is a bug waiting to happen.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claire, next time please Use Code Tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic