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

best way to calculate the percentage from SQL/JAVA
Android: Runtime exec() gives null environment?
Cant Find Database file in Eclipse for Android
Can IoC frameworks make objects with dynamic constructor parameters?
Creating a syntax tree from tokens
Java Headless exception when trying to run an executable jar in Jenkins after the build process
Unknown Column ' ', in 'field list' with hibernate and GWT
Java Persistence Criteria Predicate for OneToMany(DB ManyToMany) Relationship
Difference between JDK7 or 8? [closed]
Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread
Need Help Incrementing A Java Array by 100
Which keyword in helps to determine subclass?
Null pointer exception when connecting to database [duplicate]
How to add a new tab using FXML?
Finding if a string contains any string in a collection
How to solve those error codes from blank activity?

Categories

HOME
xamarin
openlayers
variables
bluetooth
vbscript
c#-4.0
gremlin
drivers
session
spring-cloud-stream
survey
icloud
ckeditor
yum
echarts
flyway4
indesign
portia
session-variables
python-unittest
resize
task
carthage
ghc
django-admin
textfield
jprofiler
custom-wordpress-pages
paging
java-3d
mustache.php
language-agnostic
one-to-many
sparse-matrix
fish
user-interaction
virtualdub
strncpy
xacml
buck
ejabberd-module
binary-data
xquery-3.0
gtrendsr
winrt-xaml-toolkit
catch-all
lxd
hybridauth
textmate
vxworks
jquery-bootgrid
qwerty
unspecified
ssjs
mapzen
eclipse-scout
http-live-streaming
outlook-api
slick-3.0
convertapi
sas-jmp
typescript1.8
domain-model
tizen-native-app
recursive-datastructures
nsarray
sybase-asa
qtwebview
gcsfuse
dwscript
home-directory
storekit
lua-5.1
blackberry-10
yaws
suffix-tree
php-internals
goose
microbenchmark
php-ci
sortedlist
skos
dia
sorl-thumbnail
offloading
client-side-templating
code-access-security
flask-cors
concurrent-collections
fmod
intentservice
ms-project-server-2010
.aspxauth
apc
gridcontrol
mqx
drools-planner
angularjs-controller
nsnetservice
getmessage
enter
asyncfileupload
mercurial-server
psi
locationlistener
yetanotherforum
yslow
digest-authentication
coda-slider
gallio
revisions
forums
vc90
bespin
boost-smart-ptr
ntvdm.exe

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App