import java.io.*;
import javax.swing.JFileChooser;
/** This program consists of one class with three methods and intends to use proper JavaDoc multiple line headers
* while allowing a user to choose a file of which the numbers will be
* averaged and the number of lines and mean will be displayed on the console.
*
* @author Cathy Ealy
*
*/
public class Week05 {
static int n;
//Declared a global int variable named “n” outside main
public static void main(String[] args) throws IOException {
JFileChooser jf = new JFileChooser();
int returnCode = jf.showDialog(null, null);
if (returnCode != JFileChooser.APPROVE_OPTION);{
System.exit(1);
//opens open dialog box for open file choice. Uses Chooser to allow the user to select a file.
}
String r;
r = jf.getSelectedFile().toString();//Saves the result of chooser in a string
//opens the dialog box so you can choose the file of which you want to open
double mean;
mean = calculateMean (r); //passes string into a method called calculateMean
displayResult (mean,n);
}
/**a method called calculateMean will:
1. Open the file, and read all the lines in the file (counting them as it goes, keeping the count in the global variable “n”):
2. Assume that each line of the file contains a number in a format that Double.parseDouble can handle. Use Double.parseDouble to convert each line of the file into a number
3. Add the value read from each line into a variable (initialized to zero, we call this type of variable an accumulator.)*/
public static double calculateMean(String r) throws IOException{
FileReader fr = new FileReader (r);
BufferedReader br = new BufferedReader (fr);
double sum = 0;
boolean repeat = true;
while (repeat){
String x;
x = br.readLine();
if (x != null){
double y;
y = Double.parseDouble (x);
sum = sum + y;
n = n+1;
}
else{
repeat = false;
}
}
br.close();
double mean;
mean = sum/n;
return mean;
}
/**a method called “displayResult”. (double, int) will:
1. display the mean on the console, with a string so that the user realizes that the mean is the mean.
2. Display the number of lines on the console, again, properly labeled. */
private static void displayResult(double mean, int n) {
System.out.println("The average was:" + " " + mean);
System.out.println("There were" + n + "lines in the file.");
}
}
16 Replies - 429 Views - Last Post: 24 October 2011 - 02:46 AM
#1
no output
Posted 24 October 2011 - 01:25 AM
For some reason I am not getting any kind of output with this program. I have it set to open a file that has three numbers, one on each of three lines. It should calculate the average of the numbers and state the mean and number of lines in the console (using Java Eclipse). I really don't understand java or programming very well. Eclipse doesn't show any problems or have any notes or anything for me to clue me in to what is wrong. Also if I can do anything to make my code prettier and more standardized, please let me know. Thanks.
Replies To: no output
#2
Re: no output
Posted 24 October 2011 - 01:38 AM
Hi there, you actually have a semicolon in the wrong place.
Once you remove it the code will work fine
if (returnCode != JFileChooser.APPROVE_OPTION);//here{
System.exit(1);
//opens open dialog box for open file choice. Uses Chooser to allow the user to select a file.
}
Once you remove it the code will work fine
#3
Re: no output
Posted 24 October 2011 - 01:44 AM
just changed line 17 to
and was given the following in the console:
java.lang.InterruptedException
Exception while removing reference: java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
which really means nothing to me at this point
when I have the original code I posted and remove the semicolon, I get a combined output of what I want to see and javaspeak that looks like this:
The average was: 13.666666666666666
There were3lines in the file.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
int returnCode = jf.showOpenDialog(null);
and was given the following in the console:
java.lang.InterruptedException
Exception while removing reference: java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
which really means nothing to me at this point
when I have the original code I posted and remove the semicolon, I get a combined output of what I want to see and javaspeak that looks like this:
The average was: 13.666666666666666
There were3lines in the file.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
#4
Re: no output
Posted 24 October 2011 - 01:45 AM
That wasn't the error. Check out line 18. You only need to remove the semicolon.
#5
Re: no output
Posted 24 October 2011 - 01:47 AM
removing only the semicolon I got the output
The average was: 13.666666666666666
There were3lines in the file.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The average was: 13.666666666666666
There were3lines in the file.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
#6
Re: no output
Posted 24 October 2011 - 01:49 AM
Hmm I didn't get that. Try putting even numbers like 60, 20, 40 just to test. Cause I got proper output:
The average was: 40.0
There were3lines in the file.
The average was: 40.0
There were3lines in the file.
#7
Re: no output
Posted 24 October 2011 - 01:50 AM
Think is was some error with my computer...I recopied and pasted whole thing and it works now, Thank you!
But now the output in Javadoc is different and not what I want.
But now the output in Javadoc is different and not what I want.
#8
Re: no output
Posted 24 October 2011 - 01:53 AM
You're most welcome
What is the javadoc showing and what is it supposed to be showing instead?
#9
Re: no output
Posted 24 October 2011 - 02:02 AM
You know I ran it again and got the bulky output again...it happens every two or three times I run it...
The JavaDoc should show the @author and all my notes between /** and */ in the document. It only show
JFileChooser Week05.main(String[]).jf
I am able to click on a word of my choice and it opens some other things, but before I saw it print the multi-line headers
The JavaDoc should show the @author and all my notes between /** and */ in the document. It only show
JFileChooser Week05.main(String[]).jf
I am able to click on a word of my choice and it opens some other things, but before I saw it print the multi-line headers
#10
Re: no output
Posted 24 October 2011 - 02:03 AM
Alright before we get into the javadoc concern, what are the values you are using in your txt file? Maybe I can try to simulate it here.
#11
Re: no output
Posted 24 October 2011 - 02:13 AM
12.0
13.0
16.0
The result in console should look like:
The average was: 13.6666667
There were 3 lines in the file.
13.0
16.0
The result in console should look like:
The average was: 13.6666667
There were 3 lines in the file.
#12
Re: no output
Posted 24 October 2011 - 02:27 AM
I just saved it in a notepad in my docs and selected it when it the program prompted to select file to open.
#13
Re: no output
Posted 24 October 2011 - 02:29 AM
I'm not getting the error at all
and to be honest I have never come across it before. I tried to Google around for a bit and I noticed that a similar thread on Stackoverflow
But he was developing with Swing and the error is coming from a different source.
I did the exact same thing.
But he was developing with Swing and the error is coming from a different source.
kealowa, on 24 October 2011 - 05:27 PM, said:
I just saved it in a notepad in my docs and selected it when it the program prompted to select file to open.
I did the exact same thing.
#14
Re: no output
Posted 24 October 2011 - 02:30 AM
maybe I just reran the program too soon for my computer to handle or something...it really comes up inconsistently.
How does JavaDoc work? And is there a way to make the decimal round up after so many places?
How does JavaDoc work? And is there a way to make the decimal round up after so many places?
#15
Re: no output
Posted 24 October 2011 - 02:36 AM
Not sure what I did to get this message, but I ran it and got error and some kind of dialog box opened (like when you run the pointer over something)
The box looked like the javadoc colors and said:
javax.swing.JFileChooser
The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
The box looked like the javadoc colors and said:
javax.swing.JFileChooser
The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
|
|

New Topic/Question
Reply



MultiQuote




|