1. Member
Join Date
Jan 2010
Posts
14
Rep Power
0

## `java array

i have to write a program that uses an array and from the inputted numbers i need to sort the biggest,smallest,median and mean of the numbers entered into the array
this is as far as i got

Java Code:
```class array
{
public static void main (String [] args)

{
double num1, num2, num3, num4, num5;

System.out.println ("Enter a number");
num1 = EasyIn.getDouble ();

System.out.println ("Enter another number");
num2 = EasyIn.getDouble ();

System.out.println ("Enter a third number");
num3 = EasyIn.getDouble ();

System.out.println ("Enter fourth number");
num4 = EasyIn.getDouble ();

System.out.println ("Enter a fifth number");
num5 = EasyIn.getDouble ();

//output
System.out.print(" The biggest of the five numbers, "+num1+", "+num2+", "+num3+","+num4+", "+num5);
System.out.println(", is " + max5Numbers(num1, num2, num3, num4, num5));
}

public static double max5Numbers (double s, double h, double a, double n, double e)
{
double largest = s;

if (h> largest)
{
largest = h;
}

if (a > largest)
{
largest = a;
}
if (n> largest)
{
largest = n;
}

if (e > largest)
{
largest = e;
}

return largest;
}

public static double max5Numbers (double s, double h, double a, double n, double e)
{
double smallest = s;

if (h< smallest)
{
smallest = h;
}

if (a < smallest)
{
smallest = a;
}
if (n< smallest)
{
smallest = n;
}

if (e < smallest)
{
smallest = e;
}

return smallest;
}
}```
i use a compiler called easyin

here is output

F:\Java\Assignment\array.java:58: max5Numbers(double,double,double,double,double) is already defined in array
public static double max5Numbers (double s, double h, double a, double n, double e)
^
1 error

Tool completed with exit code
any help is appreciated

2. The compiler is telling your exactly what's wrong: you're declaring a method with the same name and "signature" twice. Solution: don't do this.

3. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
i got that-thanks,but now when it compiles only the largest number is sorted,the smallest number is not done

4. Member
Join Date
Mar 2010
Posts
20
Rep Power
0
Check out bubble sort algoritm, and you may get some ideas how to implement yours.

Bubble sort - Wikipedia, the free encyclopedia

5. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
thanks will check it out

6. Member
Join Date
Mar 2010
Posts
22
Rep Power
0
When you say not done, do you mean it's not being printed, or it is printed but is incorrect?

7. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
it is only printing the largest number,it is not printing the smallest number

8. Member
Join Date
Mar 2010
Posts
20
Rep Power
0
well, you dont seem to have any line of code for printing the smallest one.

You should also edit the 3rd method´s name to min5Number, instead of max5Number

9. Member
Join Date
Mar 2010
Posts
22
Rep Power
0
Rubber duck debugging. Talk yourself through your code line by line, don't rush past anything. While you do that, think about which parts might be linked to your 'error'.

10. Originally Posted by shane123
i have to write a program that uses an array
Where's the array? Naming your class 'array' doesn't count.

kind regards,

Jos

11. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
i have been working on my code and here is where i am now,any suggestions?

Java Code:
```class accessarray
{
public static void main (String[] args)
{

int user_input;
int input_length;
int num[] = new int [3];

num[0] = EasyIn.getInt();

num[1] = EasyIn.getInt();

num[2] = EasyIn.getInt();
System.out.println ("");

System.out.println ("The nums entered are as follows");

for (int i = 0; i < 3; i++)
{
System.out.println ("Nums in position " + i +" = " +num [i]);
}

System.out.println();
System.out.println("The number is digits entered :         " + input_length);
System.out.println("The Largest Number is        :         " + largestint(user_input) );
System.out.println("The Smallest Number is       :         " + smallestint(user_input) );
System.out.println("The average Number is        :         " + average(user_input) );
System.out.println("The middle Number is         :         " + middle(user_input) );
}

public static int largestint(int [] user_input)
{
int largest_integer = 0;
for(int i=0; i<user_input.length; i++)
{
if(user_input[i] > largest_integer)
{
largest_integer = user_input[i];
}
}
return largest_integer;
}

public static int smallestint(int  [] user_input)
{
int  smallest_integer = 1;
for(int  i=0; i<user_input.length; i++)
{
if(user_input[i] < smallest_integer)
{
smallest_integer = user_input[i];
}
}
return smallest_integer;
}

public static int average(int [] user_input)
{
int result=0.0;
for(int  i=0; i < user_input.length; i++)
{
result=result + user_input[i];
}
int average=result/user_input.length;
return average;
}

public static int middle(int[] user_input)
{
int middle = user_input.length/2;
if (user_input.length%2 == 1)
{
return user_input[middle];
}
else
{
return (user_input[middle-1] + user_input[middle]) / 2;
}
}
}```
F:\Java\Lecture 9\accessarray.java:32: largestint(int[]) in accessarray cannot be applied to (int)
System.out.println("The Largest Number is : " + largestint(user_input) );
^
F:\Java\Lecture 9\accessarray.java:33: smallestint(int[]) in accessarray cannot be applied to (int)
System.out.println("The Smallest Number is : " + smallestint(user_input) );
^
F:\Java\Lecture 9\accessarray.java:34: average(int[]) in accessarray cannot be applied to (int)
System.out.println("The average Number is : " + average(user_input) );
^
F:\Java\Lecture 9\accessarray.java:35: middle(int[]) in accessarray cannot be applied to (int)
System.out.println("The middle Number is : " + middle(user_input) );
^
F:\Java\Lecture 9\accessarray.java:78: possible loss of precision
found : double
required: int
int result=0.0; // To store the total(can have a decimal point)
^
5 errors

Tool completed with exit code 1

12. The compiler error messages may seem cryptic but they really aren't and they are in fact telling you exactly what your errors are. You're passing user_input, an int variable that is never set to anything into a method that expects an array. Solution: don't do this. Instead pass the array that you have into the method. Learning to understand these error messages will help you go far. Much luck!

13. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
the program is compiling now but it printing out 0 for all the answers,any ideas?
Java Code:
```// PROGRAMMER; SHANE REDDY
// DATE; 21/3/2010
// ASSIGNMENT; WRITE ARRAY PROGRAMME

class arrays
{
public static void main(String[] args)
{
// get input and length
int userInt =0;
int input_length;

String Input1 =  EasyIn.getString();
input_length = Input1.length();

int[] user_input = new int[input_length];

for (int counter=0; counter<input_length; counter++)
{
user_input[counter] = userInt;

}
System.out.println();
System.out.println("The Largest Number is        :         " + largestint(user_input) );
System.out.println("The Smallest Number is       :         " + smallestint(user_input) );
System.out.println("The average Number is        :         " + average(user_input) );
System.out.println("The middle Number is         :         " + middle(user_input) );
}

/*
********************************************************************************************************************
********************************************************************************************************************
**************************     METHODS     *********************************************************************
********************************************************************************************************************
********************************************************************************************************************
*/
// Methods to Calculate largest value, smallest value, average and middle

// largest - returns the largest integer
public static int largestint(int [] user_input)
{
int largest_integer = 0;
for( int i=0; i<user_input.length; i++)
{
if(user_input[i] > largest_integer)
{
largest_integer = user_input[i];
}
}
return largest_integer;
}

// Smallest - returns the smallest integer
public static int smallestint(int [] user_input)
{
int smallestint;
smallestint = user_input[0];
return smallestint;
}

// Average - returns the average value (with a decimal point)
public static double average(int [] user_input)
{
double result=0.0;
int i=0;
for(i=0; i < user_input.length; i++)
{
result=result + user_input[i];
}
double average=result/user_input.length;
return average;
}
// Midlle - Gets the middle value (or closest)
public static int middle(int [] user_input)
{
int middle = user_input.length/2;  // subscript of middle element
if (user_input.length%2 == 1)
{
// If Odd number of elements -- return the middle one.
return user_input[middle];
}
else
{
// Even number -- return average of middle two
// Must cast the numbers to double before dividing.
return (user_input[middle-1] + user_input[middle]) / 2;
}
}

}```

14. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
Java Code:
```...
int userInt =0;
...
for (int counter=0; counter<input_length; counter++)
{
user_input[counter] = userInt;

}```
You are assigning 0 to all elements of your user_input array, so of course you are getting all zeroes in your results.

-Gary-

15. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
i cant get it to work,assignmnent due in morn!

16. Originally Posted by shane123
i cant get it to work,assignmnent due in morn!
Did you fix the problem that gcalvin noted above?

17. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
i tried not initializing the user_int variable to 0 but still same problem

18. It may be time to talk to your teacher, I think, to get some one-on-one time.

19. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
Originally Posted by shane123
i tried not initializing the user_int variable to 0 but still same problem
The problem is not that user_int has been initialized to 0. The problem is that it has nothing useful in it when you are assigning its value to the elements of the user_input array. Seriously, we're not trying to be mean about this, but if it is this much of a struggle getting a list of integers from user input and assigning them to an array, maybe you should be considering another field of study. Surely there must be examples in your text and study materials of getting input from users and assigning that input to variables, arrays, etc.?

-Gary-

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•