|
-
September 29th, 2011, 10:17 PM
#1
[RESOLVED] Java tutorial *help*
Here is my code I made by hand 100% so be nice if its riddled with unchecked exceptions.
Code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.Console;
import java.util.Arrays;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.FileNotFoundException;
public class Test {
public static boolean rightPlace() {
try {
BufferedReader br = new BufferedReader(new FileReader("Test2.txt"));
if(br.readLine().equals("NewObjects")) {
return true;
} else { return false; }
} catch(FileNotFoundException fnf) { System.err.println("fnf exception " + fnf.getMessage());
} catch(IOException ioe) { System.err.println("error@:"+ ioe.getStackTrace());
}
return false;
}
public static void neoWriter(String data) {
BufferedWriter bw = null;
try {
if(rightPlace() == true) {
bw.newLine();
bw.write(data);
} else { bw.newLine(); }
} catch(IOException g) { System.err.println("ERR:" + g.getStackTrace() + "\n" + g.getMessage());
}
}
public static void save(String data) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt"));
neoWriter(data);
bw.newLine();
bw.flush();
} catch(IOException e) { System.err.println("err:"+ e.getMessage());
}
}
public static void main(String[] args) {
String UInput;
System.out.println("Type:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bw = new BufferedReader(isr);
try {
UInput = bw.readLine();
save(UInput);
}catch(IOException x) { System.err.println("err:"+ x.getStackTrace());
}
}}
It compiles fine, it's just it doesn't well work when i run it, nothing happens? So clearly I made an overside but, where was it? I also, realize I could do a better job at exceptin handling, but I was just doing a simple test I wasn't sure if it was called for on a test. But, perhaps with some better exception handling (assuming it's not an unchecked exception) I might be able to find the cause, but by all means if you see a problem let me know please.
-Thanks for taking the time.
-
September 30th, 2011, 05:38 AM
#2
Re: Java tutorial *help*
Please format your code, it makes it so much easier to read when blocks of code are aligned correctly.
It compiles fine, it's just it doesn't well work when i run it, nothing happens?
Really, on my system I get the "Type:" prompt and I can enter some data.
It then falls over (throws a NullPointerException) because you are trying to read from the file before having written to it.
-
September 30th, 2011, 02:54 PM
#3
Re: Java tutorial *help*
Oh duh my bad my compiler was set to a different file. Thanks keang I will format my code better form now on. I really would appricaite any other pointers you have. Like if my code is sloppy for any other reason. Also, I plan on switching out that if else statement with a for statement to loop through the file. Anyways Thanks again.
P.S: I have the file being read on my comp I have another program this will go into. I don't know if this is smart, but I tend to make my classes sepertaly test them then impliment them.
-
September 30th, 2011, 03:07 PM
#4
Re: Java tutorial *help*
I really would appricaite any other pointers you have.
It does seem overly complex but I don't know what you are trying to achieve so you may have a good reason for doing what you have done.
Code:
if(rightPlace() == true)
It's considered bad practice to test if a boolean value equals true or false, you just need to do:
P.S: I have the file being read on my comp I have another program this will go into. I don't know if this is smart, but I tend to make my classes sepertaly test them then impliment them.
Sorry but I don't understand what you are trying to say.
-
September 30th, 2011, 03:33 PM
#5
Re: Java tutorial *help*
Thanks I'll change that boolean. Also, I was trying to say that if I have a program I'm trying to create I'll usually test methods and, certain thigns on the side before implementing it. To work out the bugs on the side the when I put it into the real program I have most of the bugs worked out. It's just been easier that way for me.
Oh yea err. I have a nullpointerexception as well now. I'll try to fix it.
Last edited by kolt007; September 30th, 2011 at 03:39 PM.
-
September 30th, 2011, 03:44 PM
#6
Re: Java tutorial *help*
 Originally Posted by keang
It then falls over (throws a NullPointerException) because you are trying to read from the file before having written to it.
hmm.. Do you mean you have to write to a file to be able to read it? You cannot read an existing file? Maybe I should show the path.
-
September 30th, 2011, 04:47 PM
#7
Re: Java tutorial *help*
Also, I was trying to say that if I have a program I'm trying to create I'll usually test methods and, certain thigns on the side before implementing it.
Oh I see, in large projects or when solving complex problems this can be a useful approach to take but generally speaking you should be writing tests harnesses for your code using something JUnit. In which case you can write and test your code in-situ.
hmm.. Do you mean you have to write to a file to be able to read it?
Yes, if you haven't written it yet how can you read from it?
-
September 30th, 2011, 05:39 PM
#8
Re: Java tutorial *help*
 Originally Posted by keang
Yes, if you haven't written it yet how can you read from it?
What if the file already exists you just want to read it?
-
September 30th, 2011, 07:32 PM
#9
Re: Java tutorial *help*
Your code looks like it should be able to read an existing file.
You need to be sure the file is where the program is looking for it to be.
Norm
-
September 30th, 2011, 09:01 PM
#10
Re: Java tutorial *help*
It is in the exact same folder/path the class file, and run program is. The error confuses me, because usually if he document to be read is in the same file it doesn't reqire a directory. Any other suggestions?
Last edited by kolt007; September 30th, 2011 at 09:21 PM.
-
September 30th, 2011, 09:06 PM
#11
Re: Java tutorial *help*
But is that where the program is looking for it?
Create a File object for the file and print its absolutePath to see where the program is looking for the file.
Norm
-
September 30th, 2011, 09:22 PM
#12
-
October 1st, 2011, 05:36 AM
#13
Re: Java tutorial *help*
What if the file already exists you just want to read it?
The file doesn't have any content at the point you are reading it, regardless of whether or nor it did when you ran the program as the first thing you do is create a FileWriter() which overwrites any existing file. If you want to append to an existing file you need to create a FileWriter using the 2 parameter constructor with the second parameter being 'true'.
The NullPointerException is not because the file doesn't exist, it's because the readline() method returns null when the end of the file is reached. You are calling the equals() returned of the returned value without checking to see if it is null.
-
October 1st, 2011, 08:18 PM
#14
Re: Java tutorial *help*
New Code:
Code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.Console;
import java.util.Arrays;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.io.BufferedInputStream;
public class Test {
public static boolean rightPlace() {
try {
BufferedReader b7 = new BufferedReader(new FileReader("Test2.txt"));
if(b7.readLine().equals("NewObjects")) {
return true;
} else { return false; }
} catch(FileNotFoundException fnf) { System.err.println("fnf exception " + fnf.getMessage());
} catch(IOException ioe) { System.err.println("error@:"+ ioe.getStackTrace());
}
return false;
}
public static void neoWriter(String data) {
try {
InputStream i7 = new BufferedInputStream(new FileInputStream("Test2.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt", true));
if(rightPlace() == true) {
bw.newLine();
bw.write(data);
} else if(i7.read() >= i7.available()) {
System.err.println("Reached End of File");
} else { bw.newLine(); }
} catch(IOException g) { System.err.println("ERR:" + g.getStackTrace() + "\n" + g.getMessage());
}
}
public static void save(String data) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("Test2.txt", true));
neoWriter(data);
bw.newLine();
bw.flush();
} catch(IOException e) { System.err.println("err:"+ e.getMessage());
}
}
public static void main(String[] args) {
String UInput;
System.out.println("Type:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bw = new BufferedReader(isr);
try {
UInput = bw.readLine();
save(UInput);
}catch(IOException x) { System.err.println("main method err:"+ x.getStackTrace());
}
}}
Specific additoins include
Code:
InputStream i7 = new BufferedInputStream(new FileInputStream("Test2.txt"));
and:
Code:
} else if(i7.read() >= i7.available()) {
System.err.println("Reached End of File");
I still get null pointer exception thrown:
ERROR
Code:
Type:
f
Exception in thread "main" java.lang.NullPointerException
at Test.rightPlace(Test.java:28)
at Test.neoWriter(Test.java:41)
at Test.save(Test.java:54)
at Test.main(Test.java:70)
Press any key to continue . . .
-
October 1st, 2011, 08:20 PM
#15
Re: Java tutorial *help*
java.lang.NullPointerException
at Test.rightPlace(Test.java:28)
What variable is null on line 28? Backtrack in your code to find out why that variable does not have a valid value.
For example this code assumes that the world is perfect:
if(b7.readLine().equals("NewObjects")) {
You should read into a String variable first, check the contents of the String for null and then compare the contents
Norm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|