vaild data?

Hello I am very new to java. This is the first course I have taken in Java so any help would be greatly appreciated! I do not have a clue on how to get my prgm to return vaild data. Should I be using RangeValidator? The following is my code. Thanks for any help!

import static java.lang.Math.*;

public class Stats

{

public static double mean(double[] xes)

{

//sum up xes

//divide total by number of elements in xes

double total = 0;

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

{

total=total + xes;

}

return(total/xes.length);

}

public static double mean(double[] xes, int n) // THIS IS MY

// -> PROBLEM AREA<--

{

//n indicates how many elements of xes

//contain vaild data

//sum up exes

//divide total by n

double total = 0;

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

{

total = total + xes;

}

return();

}

public static double maximum(double[] xes)

{

double maximum = Double.MIN_VALUE;

for (int i = 0; i >< xes.length; i++)

if (xes > maximum)

maximum = xes;

return (maximum);

}

public static double minimum (double[] xes)

{

double minimum = Double.MAX_VALUE;

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

if (xes < minimum)

minimum = xes;

return(minimum);

}

public static void main (String []args)

{

double[] xes = {1,3,5,6};

System.out.println("The Average is "+mean(xes));

System.out.println("Total divided by n is " +(xes));

System.out.println("The Maximum is " +maximum(xes));

System.out.println("The Minimum is " +minimum(xes));

}

}

[1810 byte] By [sandracs12a] at [2007-11-27 3:37:51]
# 1

Hello,

please post code inside the code tags so that it is formatted correctly and easier to read.

This doesn't work

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

{

total=total + xes;

}

To get something out of an array, you have to tell it the position of where it is.

total = total + xes[i]

>

ErikSilkensena at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 2

hey can you specify what you mean by valid data in your second method?

and in each one of you for loops, you instead of referring to the value of the xes array at the current iteration of i as xes, you must refer to it as xes[i]

so basically, in your for loops, write xes[i]

instead of xes

your probably getting a syntax error with that code?

also use code formatting when displaying code in the forums

hope it helps buddy

-Lopatin

Message was edited by:

Lopatin

Lopatina at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 3

Sorry about not posting the code inside the tags, I am unsure how to do that. I have tested my program on the mean, max, and min methods and I get the correct result, my problem is that I can't get the second method to work. I am required to get the total of the xes and then divide that total by the amount of vaild data or in others words the numbers in the string and not the letters. I hope I stated that correctly please bear with me here. I really want to understand what I need to do.

sandracs12a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 4
wait what? its double array, so there are no letters there. only variables of type double
Lopatina at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 5
Ok I need to take some time figure out how to add code tags and look at my code again. (Your right!)Thanks for the help and your time!Message was edited by: sandracs12
sandracs12a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 6

import static java.lang.Math.*;

public class Stats

{

public static double mean(double[] xes)

{

//sum up xes

//divide total by number of elements in xes

double total = 0;

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

{

total=total + xes[i];

}

return(total/xes.length);

}

//public static double mean(double[] xes, int n)

//{

//n indicates how many elements of exes contain vaild data

//sum up exes

//divide total by n

//double total = 0;

//for(int i = 0; i><xes.length; i++)

//{

//total = total + xes[i];

//}

//return();

//}

public static double maximum(double[] xes)

{

double maximum = Double.MIN_VALUE;

for (int i = 0; i >< xes.length; i++)

if (xes [i] > maximum)

maximum = xes [i];

return (maximum);

}

public static double minimum (double[] xes)

{

double minimum = Double.MAX_VALUE;

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

if (xes [i] < minimum)

minimum = xes [i];

return(minimum);

}

public static void main (String []args)

{

double[] xes = {1,3,5,6}; // < You do not have to define the xes again!

System.out.println("The Average is "+mean(xes));

//System.out.println("Total divided by n is " +(xes));

System.out.println("The Maximum is " +maximum(xes));

System.out.println("The Minimum is " +minimum(xes));

}

}

sandracs12a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 7

Ok fellows I got the code up for your viewing pleasure.

Again please bear with me. So Lopatin said that it is a double array so there are no letters, only variables of type double Got that! Now where I have put comments on the second method is where I am confused .The commented directions are from my instructor. Can you translate then what you think he means when he said vaild data?

If you run the code with my problem areas commented out it will compile and I will get the result I want. Thanks AGAIN!

Message was edited by:

sandracs12

Message was edited by:

sandracs12

sandracs12a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 8

I believe he meant to do something like this:

public static double mean(double[] xes, int n)

{

double total = 0;

for(int i = 0; i<n i++)

{

total = total + xes[i];

}

return(total/n);

}

so you count the mean of 'n' first elements of the 'xes' array.>

kokorokoa at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 9

Ok I made changes to my code. But when I go to compile I am getting an error which has to do with me having two mean methods. (geez... I hope I make sense)

This is the error:

mean(double[],int) in Stats cannot be applied to (double[])

System.out.println("The Average is "+mean(xes));

Message was edited by:

sandracs12

Message was edited by:

sandracs12

sandracs12a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 10

here is the code that works for me:

import static java.lang.Math.*;

public class Stats

{

public static double mean(double[] xes)

{

//sum up xes

//divide total by number of elements in xes

double total = 0;

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

{

total=total + xes[i];

}

return(total/xes.length);

}

public static double mean(double[] xes, int n)

{

double total = 0;

for(int i = 0; i><n ;i++)

{

total = total + xes[i];

}

return(total/n);

}

public static double maximum(double[] xes)

{

double maximum = Double.MIN_VALUE;

for (int i = 0; i >< xes.length; i++)

if (xes [i] > maximum)

maximum = xes [i];

return (maximum);

}

public static double minimum (double[] xes)

{

double minimum = Double.MAX_VALUE;

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

if (xes [i] < minimum)

minimum = xes [i];

return(minimum);

}

public static void main (String []args)

{

double[] xes = {1,3,5,6}; // < You do not have to define the xes again!

System.out.println("The Average is "+mean(xes));

//System.out.println("Total divided by n is " +(xes));

System.out.println("The Maximum is " +maximum(xes));

System.out.println("The Minimum is " +minimum(xes));

}

}

kokorokoa at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 11
I tried to compile the code I received errors for line 23 and 35?Message was edited by: sandracs12
sandracs12a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 12
The forum can't format code correctly sofor(int i = 0; i><n ;i++)should befor(int i = 0; i<n ;i++)Think before you paste.
pbrockway2a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 13

oh sry I dont know how it s possible but I have pasted the wrong code. sry.

line 23: "for(int i = 0; i><n ;i++)"change >< into <

line 35: same mistake.

btw you need to learn some basics such as:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

cheers.

kokorokoa at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 14

> oh sry I dont know how it s possible but I have pasted the wrong code

@kokoroko The code you pasted was OK. My comment was more aimed at the OP who should check that code is understandable before using it. (And >< is not an understandable operator).

The workaround for the bogus closing angle bracket bug is to always use &lt; instead of < - both in code and in normal text.

pbrockway2a at 2007-7-12 8:41:09 > top of Java-index,Java Essentials,New To Java...
# 15
Thanks I am going to take a good look at the link!
sandracs12a at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...
# 16
kokoroko... thanks for the help I will check out the link!
sandracs12a at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...
# 17

kokoroko i believe the second condition in the second method of your code has a typo.

also, just a suggestion, but i think that "valid data" may refer any non zero data in the array. so if an array were to look like this {4, 6, 0, 3, 0, 0, 9, 0, 2, 3} then n would be equal to 6. so i think it would be right to find the sum of all the values in the array(including the zeros) and divide by n. that way all meaningful data will be averaged and the zeros wont be counted as part of the average

considering korokos method only finds the average of the first n values, i think this would be more effective?

Lopatina at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...
# 18
pbrockway2... I thought the code looked peculiar, however the reason I am on this forum is for the help from others who are fimilar with java. So I took the advice in good honor. I want to understand what I am doing and not just copy and paste.Thanks again kokoroko!
sandracs12a at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...
# 19
This makes sense, my problem is I was unsure as to what my instructor was asking. This clarifies my confusion thank agian Lopatin!
sandracs12a at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...
# 20

> pbrockway2... I thought the code looked peculiar, however the reason I am on this

> forum is for the help from others who are fimilar with java. So I took the advice in

> good honor.

In which case you would have found my correction to the forum's code mangling useful. You're welcome.

> I want to understand what I am doing and not just copy and paste.

Good. Me too; and, for me at any rate, that means understanding the stuff I compile, and understanding the compiler messages I (too frequently) get back when I get it wrong.

pbrockway2a at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...
# 21
= )
sandracs12a at 2007-7-21 20:51:27 > top of Java-index,Java Essentials,New To Java...