Write some JAVA for me please, not working the way I wanted it too...

Here's what I am getting when I go to run it:

C:\Documents and Settings\Brad.HOME\Desktop\FINAL>java project1

Usage: project1 [Array File Path]

C:\Documents and Settings\Brad.HOME\Desktop\FINAL>

What I need is for it to ask me to enter the location of the numbers file and then calculate the Max, Min, Mean, etc off of that file I enter in. Right now it just says:

Usage: project1 [Array File Path]

-java below-

import java.io.*;

public class project1 {

/**

* Array File Path

*/

private String location;

/**

* set Location

*

* @param location String

*/

public void setLocation(String location) {

this.location = location;

}

/**

* Program initialization

*/

public project1() {

int ar[] = new int[50];

ReadFile(location, ar);

sort(ar);

System.out.println("Minimum= " + ar[0]);

System.out.println("Maximum=" + ar[ar.length - 1]);

System.out.println("Mean= " + mean(ar));

System.out.println("Mode= " + modal(ar));

System.out.println("Median= " + median(ar));

System.out.println("Std Dev= " + stdDev(ar));

}

/**

* Main program

*

* @param args String[]

*/

public static void main(String args[]) {

if (args.length == 1) {

new project1().setLocation(args[0]);

}

else {

System.out.println("Usage: project1 [Array File Path]");

}

}

/**

* Calculate the mean

*

* @param ar int[]

* @return double

*/

public double mean(int ar[]) {

int sum = 0;

for (int i = 0; i < ar.length; i++) {

sum += ar;

}

return (sum / ar.length);

}

/**

* Calculate the mode/modal

*

* @param a int[]

* @return int

*/

public int modal(int a[]) {

int n;

int freq = 0;

int Mfreq = 0;

int Mn = a[0];

for (int i = 0; i < a.length; i++) {

n = a;

for (int j = 0; j < a.length; j++) {

if (a[j] == n) {

freq++;

}

}

if (freq > Mfreq) {

Mn = n;

}

}

return Mn;

}

/**

* Calculate the median

*

* @param a int[]

* @return int

*/

public int median(int a[]) {

if (a.length % 2 == 0) {

int x = a[a.length / 2];

int y = a[ (a.length + 2) / 2];

return (x + y) / 2;

}

else {

return a[ (a.length + 1) / 2];

}

}

/**

* Calculate Standard Deviation

*

* @param a int[]

* @return double

*/

public double stdDev(int a[]) {

int s2 = 0;

int s = 0;

for (int i = 0; i < a.length; i++) {

s2 += (a * a);

s += a;

}

return Math.sqrt( (s2 - (s * 2) / a.length) / (a.length - 1));

}

/**

* Sort integer array

*

* @param a int[]

*/

public void sort(int a[]) {

int tmp;

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length; j++) {

if (a[j] > a[j + 1]) {

tmp = a[j];

a[j] = a[j + 1];

a[j + 1] = tmp;

}

}

}

}

/**

* Read integer array from specified file

*

* @param filename String

* @param ar int[]

* @return int

*/

public int ReadFile(String filename, int ar[]) {

File infile = new File(filename);

int value, n = 0;

try {

BufferedReader reader = new BufferedReader(new FileReader(infile));

String line = null;

while ( (line = reader.readLine()) != null) {

value = Integer.parseInt(line);

ar[n++] = value;

}

return n;

}

catch (IOException e) {

System.err.println("Problem reading input file!");

System.exit(1);

}

return n;

}

}

[4010 byte] By [vonoventwina] at [2007-10-2 11:23:16]
# 1
Sorry, we can't do your assignments for you. Ask specific questions if it's not working the way you want.
ChuckBinga at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

Yeah thanks a lot big chucky! That's exactly what I wanted. You to write my whole assignment, NOT! I wanted to know how to prompt the user to enter a file location instead of the file Temp.txt actually being inside the code. I know how to println to Prompt them to enter the file and how to ReadFile once they input it, but something is screwed up and I don't know what or I wouldn't be asking for help. Thanks for the great tip Chucky, you deserve to have all my Duke points b/c you were just so so very helpful. Go eat dirt!

vonoventwina at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

Original code was like this before I tried to make it prompt to input file instead....

public project1() {

int ar[] = new int[25];

ReadFile("C:\\Temp.txt", ar);

sort(ar);

System.out.println("Minimum= " + ar[0]);

System.out.println("Maximum=" + ar[ar.length - 1]);

System.out.println("Mean= " + mean(ar));

System.out.println("Mode= " + modal(ar));

System.out.println("Median= " + median(ar));

System.out.println("Std Dev= " + stdDev(ar));

}

vonoventwina at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

Please use code tags when posting code. There is a button labeled code right above the text box where you type in your post.

Your first problem occurs herepublic static void main(String args[]) {

if (args.length == 1) {

new project1().setLocation(args[0]);

}

else {

System.out.println("Usage: project1 [Array File Path]");

}

}

This code executes when you first launch the application. It checks to see if there were any arguments in the comman line. If the number of arguments is not equal to 1, then you get the Usage output. You must launch the program with one argument.

atmguya at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
> Yeah thanks a lot big chucky! . . .This type of response guarantees that you'll receive minimal assistance. It''s a pathetic attempt at flaming. . .
ChuckBinga at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

> Please use code tags when posting code. There is a

> button labeled code right above the text box

> where you type in your post.

>

> Your first problem occurs herepublic static

> void main(String args[]) {

>if (args.length == 1) {

>new project1().setLocation(args[0]);

>}

>else {

> System.out.println("Usage: project1 [Array File

> y File Path]");

>}

> }

This code executes when you first launch the

> application. It checks to see if there were any

> arguments in the comman line. If the number of

> arguments is not equal to 1, then you get the Usage

> output. You must launch the program with one

> argument.

How in the world do I launch the program with an argument?

vonoventwina at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7
> > Yeah thanks a lot big chucky! . . .> > This type of response guarantees that you'll receive> minimal assistance. It''s a pathetic attempt at> flaming. . .Sorry chuckster, I didn't mean to hurt anyones feelings.
vonoventwina at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8
> > output. You must launch the program with one> > argument.> > How in the world do I launch the program with an> argument?What does it say? "Usage: project1 [Array File Path]"So you type in java project1 some_file_path
atmguya at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 9

> Please use code tags when posting code. There is a

> button labeled code right above the text box

> where you type in your post.

>

> Your first problem occurs herepublic static

> void main(String args[]) {

>if (args.length == 1) {

>new project1().setLocation(args[0]);

>}

>else {

> System.out.println("Usage: project1 [Array File

> y File Path]");

>}

> }

This code executes when you first launch the

> application. It checks to see if there were any

> arguments in the comman line. If the number of

> arguments is not equal to 1, then you get the Usage

> output. You must launch the program with one

> argument.

I tried java project1 C:\\Temp.txt, but it still ends up with a NullException handler. I edited the code and added a catch for it so it returns File doesn't exist, but I swear it does and I'm pointing to it correctly...

vonoventwina at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 10
> new project1().setLocation(args[0]);You call the constructor first.You call the method second.So what value does location have in the following line in the constructor?> ReadFile(location, ar);
jschella at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 11
thanks I got in working www.vonoven.com/project1.java Now I have to get the Math corrected....
vonoventwina at 2007-7-13 4:26:11 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...