java


Calculator and using arrays


I am trying to get this working so when a user types in numbers it in turn outputs the max, min, sum and avg of those numbers. The first number is the amount of numbers going to be entered. I am supposed to call methods to do each calculation and use the main method to print. When I go to enter something in into just prints out 0.00 and not the actual min, max, sum and avg. It prints out 0.00 equal to the first number typed in which it shouldn't do. Any help would be appreciated.
Sample Input
5 1.0 2.0 3.0 4.0 5.0
Sample Output
15.00 3.00 1.00 5.00
My current code
import java.util.Arrays;
import java.util.Scanner;
public class ManyNumbers {
public static void main(String args[]) {
double i,n;
Scanner input = new Scanner(System.in);
while(input.hasNext()){
n=input.nextInt();
double a[] = new double [(int) n];
for(i=0;i<n;i++){
a[(int) i] = input.nextDouble();
double max1 = max (a);
double min1 = min (a);
double sum1 = sum (a);
double avg= sum1/n;
System.out.printf("%10.2f",min1, max1, sum1, avg);}
}
}
private static double sum(double[] array){
double sum = 0;
for (double e : a) sum += e;
return sum;}
private static double min(double[] a){
Arrays.sort(a);
double min =a[0];
return min;}
private static double max(double[] a){
Arrays.sort(a);
double max= a[a.length -1];{
return max;}
}
}
Here is the fully corrected version:
So I fixed your input so that it could be varied and fixed you methods as well.
import java.util.Arrays;
import java.util.Scanner;
public class ManyNumbers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int arraysize = in.nextInt();
Double[] array = new Double[arraysize];
for (int i = 0; i < arraysize; i++) {
array[i] = in.nextDouble();
}
System.out.println(sum(array) + " " + min(array) + " " + max(array));
}
private static double sum(Double[] array) {
double sum = 0;
double i = 0;
Double a[] = new Double[array.length];
a = array;
for (double e : a)
sum += e;
return sum;
}
private static double min(Double[] array) {
Double a[] = new Double[array.length];
a = array;
Arrays.sort(a);
double min = a[0];
return min;
}
private static double max(Double[] array) {
Arrays.sort(array);
double max = array[array.length - 1];
return max;
}
}
I tried to read that, but the indentation is really weird, tried to indent it:
EDIT: Ok so I am done indenting, the problem seems to be that you set N to 0 then create an array of size n (which is 0). You've done this in 3 methods.
import java.util.Arrays;
import java.util.Scanner;
public class ManyNumbers {
public static void main(String args[]) {
double i,n;
Scanner input = new Scanner(System.in);
while(input.hasNext()){
n=input.nextInt();
double a[] = new double [(int) n];
for(i=0;i<n;i++){
a[(int) i] = input.nextDouble();
double max1 = max (a);
double min1 = min (a);
double sum1 = sum (a);
double avg= sum1/n;
System.out.printf("%10.2f",min1, max1, sum1, avg);
}
}
}
private static double sum(double[] array){
double sum = 0;
double i,n = 0; //N is zero
double a[] = new double [(int) n];//Creating an array of size n(0)
for (double e : a)
sum += e;
return sum;
}
private static double min(double[] array){
double i,n = 0; //N is zero
double a[] = new double [(int) n]; //Creating array of size N
Arrays.sort(a);
double min =a[0];
return min;
}
private static double max(double[] array){
double i,n = 0; // N is zero
double a[] = new double [(int) n]; //Creating array of size 0
Arrays.sort(a);
double max= a[a.length -1];
{//Why?
return max;
}
}
}
EDIT2: I think i may have found your problem. It seems like you thought nfrom main() was global scope. You then defined another local variable for each method with the same name, which made you think it was the same variable, which it wasnt.
I would have posted the right way to do it but i dont have the time, i'm sorry.
There are definitely a number of issues with the code but it seems to me that the very first initialization of the double array a, should not be declared every time the loop runs. You're not adding all the user input to a single array rather creating new arrays every time the user enters input. You'd have to write code to ask how many numbers the user is inputing or hardcode it before you declare the double array. Something like this:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of doubles to add to the array");
n=input.nextInt();
double a[] = new double [(int)n];
That will only take care of your first issue. I would also use a for loop with a counter to add the doubles to the array after that. The methods need work too but this will hopefully give you a start. Good luck!
This is the final code that worked with the formatting that was required of it.
import java.util.Arrays;
import java.util.Scanner;
public class ManyNumbers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int arraysize = in.nextInt();
Double[] array = new Double[arraysize];
for (int i = 0; i < arraysize; i++) {
array[i] = in.nextDouble();
}
double sum1 = sum(array);
double max1 = max(array);
double min1 = min(array);
double avg= sum1/arraysize;
System.out.printf("%10.2f",sum1);
System.out.printf("%10.2f",avg);
System.out.printf("%10.2f",min1);
System.out.printf("%10.2f",max1);
System.out.println();}
}
private static double sum(Double[] array) {
double sum = 0;
double i = 0; // N is zero
Double a[] = new Double[array.length];
a = array;
for (double e : a)
sum += e;
return sum;
}
private static double min(Double[] array) {
Double a[] = new Double[array.length];
a = array;
Arrays.sort(a);
double min = a[0];
return min;
}
private static double max(Double[] array) {
Arrays.sort(array);
double max = array[array.length - 1];
return max;
}
}

Related Links

Is it bad design using static variables for core parts instances?
Constructor overloading in Java - when to use it? [closed]
How to add external jar files to maven shade plugin
Unable to resolve target 'android-21'
Passing list of data from JSP to Set in Java
Export and Import apacheds data into LDIF programmatically from java
How do I use a point class, rational number class, and line class to Solve a system of 2 linear equations?
how to get a roman numeral calculator to work with negative numbers
Where did I get the example code in Maven?
No line number in Log4j trace level
NoSuchBeanDefinitionException for #Repository #Component annotations
Command line options to batch file (Java application)
Java - How to list files on mapped network drive?
creating a class to store some integers Java [duplicate]
iReport 2.0.4 Connection - ClassNotFoundException
getting all pid's from pstree

Categories

HOME
wso2-am
netbeans
keras
activiti
hp-exstream
raspberry-pi
octobercms
webstorm
xmpp
q
getelementsbytagname
cvs
icloud
windows-server
ezpublish
azure-storage-tables
append
kibana-4
metatrader4
here-api
fancybox-3
n-gram
vb.net-2010
quickfix
worldwind
pugjs
crystal-reports-2008
karma-jasmine
visual-composer
one-hot-encoding
intentfilter
ehcache
graphicsmagick
facebook-instant-articles
cultureinfo
claims-based-identity
smb
user-interaction
pingfederate
saas
framemaker
hybridauth
avcapturesession
android-nestedscrollview
main
restlet
serve
gesture
azure-ml
gulp-sourcemaps
powershell-dsc
android-mediaprojection
nxlog
thin
scrollable
revapi
jedis
mu
mplayer
typed-lambda-calculus
nsarray
ifs
mcafee
quartz-composer
skobbler-maps
firebaseui
fadeout
tableau-online
service-accounts
truevault
c++03
nstableviewcell
camanjs
coldbox
winddk
issuu
mesa
jboss-weld
message-driven-bean
xsockets.net
dotnetnuke-5
android-hardware
factory-method
browser-detection
runas
parametric-equations
qtkit
nsdatecomponents
servicehost
fluent-interface
dmx512
revisions
castle-monorail
user-friendly
zend-tool
adrotator
simpletest
phonon
noscript

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App