• Post Reply Bookmark Topic Watch Topic
  • New Topic

"File not found" in my I/O text file program  RSS feed

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still a beginner at txt files, built my program for the user to enter a name and the program searches for the name (out of a thousand) in the girl name and boy name txt file. The txt files are pretty long. Literally a thousand lines so i won't bother posting it, but how does the program exactly locate and read the files? If you need the driver and my name class I can provide it and maybe something in my code is not working but I'm pretty sure it has something to do with the location or something with my txt files...

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll just provide them without the txt files:

Main Class:

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

public class NameRank
{
public static void main(String[] args)
{
Scanner boyInputStream;
Names[] boyNameArray;

Scanner girlInputStream;
Names[] girlNameArray;


try
{
boyInputStream = new Scanner(new FileInputStream("boynames.txt"));
boyNameArray = new Names[1000];
girlInputStream = new Scanner(new FileInputStream("girlnames.txt"));
girlNameArray = new Names[1000];

//input all boy and girl names into an array
for(int i = 0; i < 1000; i++)
{
boyNameArray[i] = new Names();
boyNameArray[i].setName(boyInputStream.next());
boyNameArray[i].setNumber(boyInputStream.nextInt());

girlNameArray[i] = new Names();
girlNameArray[i].setName(girlInputStream.next());
girlNameArray[i].setNumber(girlInputStream.nextInt());
}

boyInputStream.close();
girlInputStream.close();

// get name input from user
System.out.println("To find a ranked name for a girl or boy in 2003");
System.out.println("enter a name and we will search the entered name: ");
Scanner kboard = new Scanner(System.in);
String userChoice = kboard.nextLine();
userChoice = (userChoice.substring(0, 1).toUpperCase()) +
userChoice.substring(1);

// search through both arrays to find match(es)
boolean boyFound = false;
boolean girlFound = false;
for(int i = 0; i < 1000; i++)
{
if(boyNameArray[i].getName().equals(userChoice))
{
System.out.println(userChoice +
" is ranked " + (i + 1) +
" in popularity among boys with " +
(boyNameArray[i].getNumber() + 1) +
" namings.");
boyFound = true;
}

if(girlNameArray[i].getName().equals(userChoice))
{
System.out.println(userChoice +
" is ranked " + (i + 1) +
" in popularity among girls with " +
(girlNameArray[i].getNumber() + 1) +
" namings.");
girlFound = true;
}
}

if(boyFound && !girlFound)
System.out.println(userChoice + " is not ranked.");
else if(!boyFound && girlFound)
System.out.println(userChoice + " is not ranked.");
else if(!boyFound && !girlFound)
System.out.println(userChoice + " is not ranked in " +
"either boys or girls");
}
catch(FileNotFoundException e)
{
System.out.println("FileNotFound");
}
}
}
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name Class:


public class Names
{
private String name;
private int number;

public Names()
{
name = "Unknown name";
number = 0;
}

public Names(String theName, int theNumber)
{
name = theName;
number = theNumber;
}

public void setName(String instName)
{
name = instName;
}

public void setNumber(int instNumber)
{
number = instNumber;
}

public String getName()
{
return name;
}

public int getNumber()
{
return number;
}

public String toString()
{
return (name + " " + number);
}
}
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George,

Still learning Java so i couldn't and shouldn't comment on your actual code.
It seems to me this would only work if your .txt files were in your projects root directory.
Is this what you are asking?

Regards,
Daniel
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I believe so. I just wanna know why I can't access the files. It's on my workspace but I'm guessing there is more to it?
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your workspace is where your projects are kept, your root directory would be the Folder with your projects name?

This should tell you your root if you append it to the class you are working in.

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I tried it out and tested it. All it said was null... =/
I mean I looked in the folder where my name rank java files are at and the txt files are there too!

Sorry it's just frustrating lol
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running this program - by entering a command on the command line? In that case those files should be in the same directory from where you're starting the program.

If you're starting the program some other way, tell us the details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!