Results 1 to 4 of 4
- 04-18-2011, 11:42 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Finding MAX value from "file.txt"
I'm trying to find the MAX value for...let's say 600 integers from "file.txt" by using an array. And oh, I've only learn until array so far.
"file.txt" contains like this:
2.3
3.4
6.0
...
Here's what I have so far:
Java Code:public static void main(String[] args)throws FileNotFoundException{ Scanner console = new Scanner(System.in); System.out.print("Please enter your file name: "); String name = console.nextLine(); Scanner input = new Scanner(new File(name)); int totalNum = input.nextInt(); System.out.println("There are " + totalNum + " in your file."); double[] count = new double[1000]; //Putting list of integers from file into an array? int i = 1; while (input.hasNextDouble()){ count[i++] = input.nextDouble(); } double max = getMaximum(count); //Calling getMaximum System.out.println("Max: " + max); } public static double getMaximum(double[] count){ double max = 0; for(int i = 1; i <= count.length; i++){ if(count[i] > max){ max = input[i]; } } return max; }Last edited by masterboemi; 04-19-2011 at 12:00 AM.
- 04-18-2011, 11:49 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
What is the question?
Does your code compile? If you cannot understand a compiler message, copy and post the entire thing. Does the program show unwanted behaviour at runtime? including runtime exceptions or other unexpected output? In that case describe both the actual and the expected behaviour.
- 04-18-2011, 11:58 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Oh oops, well, there are no errors but the code won't print the MAX value (double value).
- 04-19-2011, 12:09 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Java Code:if([b]count[/b][i] > max){ max = [b]input[/b][i]; }
This doesn't look right - but if the code actually compiled it's hard to say without seeing the whole code. Also, what is the output?
-----------------
It strikes me that getMaximum() works it's way through the array looking at one element at a time. It might be a better idea not to use this method as-is but, rather, do something similar to the values in the file: ie don't make an array, just keep track of the biggest value seen so far as you read the file.
Similar Threads
-
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 09:55 AM -
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks