Results 1 to 19 of 19
Thread: `java array
- 03-19-2010, 02:00 AM #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
i use a compiler called easyinJava 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; } }
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
-
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.
- 03-19-2010, 02:09 AM #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
- 03-19-2010, 02:16 AM #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
- 03-19-2010, 02:22 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
thanks will check it out
- 03-19-2010, 02:35 AM #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?
- 03-19-2010, 02:38 AM #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
- 03-19-2010, 02:42 AM #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
- 03-19-2010, 10:16 AM #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'.
- 03-19-2010, 10:53 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2010, 02:52 AM #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?
F:\Java\Lecture 9\accessarray.java:32: largestint(int[]) in accessarray cannot be applied to (int)Java Code:class accessarray { public static void main (String[] args) { int user_input; int input_length; int num[] = new int [3]; System.out.println ("Please enter num 1"); num[0] = EasyIn.getInt(); System.out.println ("Please enter num 2"); num[1] = EasyIn.getInt(); System.out.println ("Please enter num 3"); 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; } } }
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
-
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!
- 03-21-2010, 11:11 PM #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; System.out.print("Please Enter integers (e.g 1234):"); 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; } } }
- 03-21-2010, 11:30 PM #14
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You are assigning 0 to all elements of your user_input array, so of course you are getting all zeroes in your results.Java Code:... int userInt =0; ... for (int counter=0; counter<input_length; counter++) { user_input[counter] = userInt; }
-Gary-
- 03-21-2010, 11:56 PM #15
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
i cant get it to work,assignmnent due in morn!
-
- 03-22-2010, 12:36 AM #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
-
It may be time to talk to your teacher, I think, to get some one-on-one time.
- 03-22-2010, 12:42 AM #19
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
Similar Threads
-
Java Array Help
By md69holla in forum New To JavaReplies: 14Last Post: 02-15-2010, 06:02 PM -
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07-31-2009, 06:24 PM -
how to convert a Java array to a java stack?
By pompeez in forum New To JavaReplies: 2Last Post: 08-13-2007, 02:41 PM -
Help with array in java
By coco in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:03 AM -
Help with array and files in java
By fernando in forum New To JavaReplies: 1Last Post: 07-31-2007, 07:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks