Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java beginner - I am see some crazy errors with file reader

Status
Not open for further replies.

VBBro

Programmer
Jun 25, 2004
4
US
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 ??????????????????????????????????????????????????????????????????
 
You're getting that because of the following line:
Code:
s2 = in.toString();
That line of code is just returning "java.io.BufferReader@1ddebc3" string that you later print out. You need to do readLines in order to read in the file data.

 
Problem 1) java.lang.NoClassDefFoundError: practice/SortF (wrong name: SortF)
The reason your IDE is barfing is that you have your code in a package named "practice" - but your class is not declared within a package - ie it does not have the declaration :
Code:
package practice;

Problem 2)
The method "toString()" in the class java.lang.Object returns the following concatenated string :
- The class package and name
- An @ symbol
- The JVM allocated hash code

This is true unless a class overrides the toString() method. Which IO stream classes do not, because it makes no sense. If you want to read the input from an IO stream, you should call the relevant read*() methods, ie :

Code:
BufferedReader br ...
String line = "";
while ((line = br.readLine()) != null) {
  System.out.println(line);
}

Perhaps you should take a Java IO tutorial :
--------------------------------------------------
Free Database Connection Pooling Software
 
I can't get the package thing to work but this code works great in DOS! Thanks for you help good people!

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

//package practice;
import java.io.*;
import java.util.*;

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

try {

System.out.println("Enter File Name! ");
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));
String FFile;
FFile = stdin.readLine();

BufferedReader in = new BufferedReader(
new FileReader(FFile));
String[] F = new String[9];
for(int i=0; i<9; i++) F = 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) {

}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top