C:\slop>java TestGreatestnNum
Enter a value to specify no. of inputs(numbers) and find their greatest - 0 to exit:
5
You have input: 5
Enter value:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at TestGreatestnNum.main(TestGreatestnNum.java:29)
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
S.D. MADHAN
Not many get the right opportunity !
You may do the following.
1. Move line number 10 to line number 24
2. Move line 54 below current line number 34
3. Comment or remove current lines 35 to 57
4. Add the following lines, after the current line number 57
1. Arrays.sort(arr);
2. result = arr[arr.length-1];
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor wrote:First of all, if this is a homework assignment, it probably defies the purpose of the assignment.
Rob Spoor wrote:I wouldn't sort the array just to find the largest value.
Rob Spoor wrote:Secondly, sorting is at least an O(n log n) operation, whereas a simple loop is an O(n) operation. That means that as the arrays grow larger, the simple loop becomes more and more efficient compared to the sorting.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
1. int max = -1; // assuming you only have positive numbers
2. for (int value: arr)
3. {
4. if (max < value) max = value;
5. }
# If you still haven't got it working, I would make the following suggestions:
1: Forget what you have done to date.
# 2: Write down a series of numbers, and go through them by hand looking for the largest (or smallest) values.
# 3: Write down exactly what you are doing, in words of one syllable.
# 4: Now you have pseduocode, you can easily change that to real code
Hint: When you only have one number n, the smallest number seen to date is n, and the lrgest number seen to date is n. When you have two different numbers, that will change.
Campbell Ritchie wrote:That for-each loop looks so much neater and more elegant, but I can see a possible enhancement
Campbell Ritchie wrote:
That means you start off guessing that the first value is the largest. You can do exactly the same for minimum; in fact it is probably best to do it in the same loop. You may wish to start with the subsequent values after my assignmentThat will give the correct values for any array size except a 0-length array, which will produce an ArrayOutOfBoundsException.
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|