Results 1 to 4 of 4
  1. #1
    masterboemi is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default 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 01:00 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    masterboemi is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Oh oops, well, there are no errors but the code won't print the MAX value (double value).

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. Replies: 6
    Last Post: 02-10-2011, 10:55 AM
  2. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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