This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileNotFoundException

 
Daenen Omega
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why won't java find the file "FFile.txt" in this code? The file is located in the same directory as the .java file?!? I am going INSANE!!!

----------------------------------------------------------------------------

import java.io.*;
import java.util.*;

public class SortF {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(
new FileReader("FFile.txt"));
String s2 = new String();
s2 = in.toString();
in.close();

StringTokenizer FTokens = new StringTokenizer(s2);

String[] F = {FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken()};

System.out.println();

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

Arrays.sort(F);

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

}
}
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Change that to...

...please

That won't fix your problem, but

Perhaps you have extensions disabled, and it is really FFile.txt.txt or something similar?

edit: broke tables
[ July 24, 2004: Message edited by: Darin Niard ]
 
Daenen Omega
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the split() tip and I added and IOException but now I have this:
----------------------------------------------------------------------------
import java.io.*;
import java.util.*;

public class SortF {
public static void main(String[] args) {

try {

BufferedReader in = new BufferedReader(
new FileReader("FFile.txt"));
String s2 = new String();
s2 = in.toString();
in.close();

String[] F = s2.split(" ");
System.out.println();

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

Arrays.sort(F);

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

} catch (IOException ioe) {

}
}
}
----------------------------------------------------------------------------
and these are my errors:
in NetBeans:

java.lang.NoClassDefFoundError: practice/SortF (wrong name: SortF)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
Exception in thread "main"

and in DOS I get:

java.io.BufferReader@1ddebc3

java.io.BufferReader@1ddebc3

What the ???
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daenen Omega:
s2 = in.toString();

...

java.io.BufferReader@1ddebc3

You should be using readLine() not toString().
 
Daenen Omega
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started to learn Java so I am not really saying anything against it, yet. However, I just crushed this problem using C++ in record time:
-----------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char str[9][80];
char t[80];
char fname[80];

cout << "ENTER COMPLETE FILE NAME: ";
cin >> fname;

ifstream in(fname);
if(!in) {
cout << endl << "YOU'RE SO WRONG!" << endl << endl;
system("PAUSE");
return 1;
}

for(int i=0; i<10 ; i++) in >> str[i];

in.close();
int size = 9;

//use C++ bubble sort to sort the array.

for(int a=1; a<size; a++)

for(int b=size-1; b>=a; b--) {

if(strcmp(str[b-1],str[b])>0) {

strcpy(t, str[b-1]);
strcpy(str[b-1], str[b]);
strcpy(str[b], t);
}
}

for(int x=0; x<10; x++) cout << str[x] << endl;

system("PAUSE");
return 0;
}
 
Daenen Omega
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tada - Java!
----------------------------------------------------------------------------
import java.io.*;
import java.util.*;

public class SortF {
public static void main(String[] args) {

try {

BufferedReader in = new BufferedReader(
new FileReader("FFile.txt"));
String[] F = new String[9];
for(int i=0; i<9; i++) F[i] = in.readLine();
in.close();

System.out.println();

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

Arrays.sort(F);

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

} catch (IOException ioe) {

}
}
}
----------------------------------------------------------------------------
I would like to thank all of the goodly people that helped me through this difficult time - Thanks!

One last quandry though. This program runs great in DOS but in NetBeans the output is BLANK??? Any thoughts?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daenen Omega:
One last quandry though. This program runs great in DOS but in NetBeans the output is BLANK??? Any thoughts?


If you put an ioe.printStackTrace() in the catch block, you will probably see the FileNotFoundException again.

I guess that Netbeans is running your program in a different directory. I don't know this IDE, but you should be able to configure that somewhere - it's probably called "working directory" or something.
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forget which method you should use, but there has got to be a method that tells you what the current directory is. You might want to start by looking at System.getProperty("user.dir");

RK
 
Marilyn de Queiroz
Sheriff
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll probably have a better chance of getting IDE (Netbean) -specific help in the IDE forum.
 
Sonny Pondrom
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daenen

You should checkout the tutorial on input/output
 
Daenen Omega
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your help friends!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic