qqq
qqq

Reputation: 13

Find max value from input text file in java

Im trying to get the max number in a certain text file the user inputs. I also put it into separate methods. Heres what I have so far:

public static void FindMax(String file)throws IOException{
 int maximum = 0;
 Scanner fileScanner = new Scanner(new File(file)); {  
            int big = fileScanner.nextInt();
            while (fileScanner.hasNextInt()) {
                int num = fileScanner.nextInt();
                if(num > big) {
                   maximum++;
                     System.out.println(num);;
                }
           }
  }
}
public static void main(String[] args)throws IOException{

    Scanner keyboard = new Scanner(System.in);
     String file;                                             

      System.out.print("Enter file: ");                  
       file = keyboard.nextLine();                            

       FindMax(file);

}

The output is printing all the content in the text file except the first value, instead of printing the maximum. For example if the text file is:

1
2
3
4
5

It only prints 2,3,4, and 5 and I don't know why.How can I get the max value? Id appreciate any help/advice. Thanks in advance.

EDIT: All of you guys are saying similar solutions but when I try them, it just prints the same output. Im very confused.

Upvotes: 1

Views: 5096

Answers (5)

jrook
jrook

Reputation: 3519

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FindMax {
    public static void findMax(String file) throws IOException {
        Scanner fileScanner = new Scanner(new File(file));
        int max = fileScanner.nextInt();
        while (fileScanner.hasNextInt()) {
            int num = fileScanner.nextInt();
            if (num > max) {
                max=num;
            }
        }
        System.out.println(max);
        fileScanner.close();
    }

    public static void main(String[] args) throws IOException {

        Scanner keyboard = new Scanner(System.in);
        String file;

        System.out.print("Enter file: ");
        file = keyboard.nextLine();

        findMax(file);
        keyboard.close();
    } 
}

I fixed logical errors in your code. I am not sure why you defined the maximum variable. It is not really used in any meaningful way in your code. You might want to consider changing the return type of your method to "int" and return the found maximum.

Also, method names must always start with small letters: http://www.javatpoint.com/java-naming-conventions

You should also always correctly close your resources: How to Correctly Close Resources

Upvotes: 0

Razib
Razib

Reputation: 11163

Whenever you get the any num bigger than the big then you should update the big with the `num'. like this -

while (fileScanner.hasNextInt()) {
   int num = fileScanner.nextInt();
   if(num > big) {
      big = num;
   }
} 

Then after finishing the loop you will get your max number. Just print the big -

System.out.println(big);

Upvotes: 0

FilipRistic
FilipRistic

Reputation: 2821

What you want to do is inside while loop:

if(num > big) {
    big = num;        
}

Then when you finish with while loop you just print big:
System.out.println(big);
You don't need that maximum since you are not using it for anything, but if you should first fix syntax errors(wrong placed and missing brackets) or maybe you didnt copy code properly.

Upvotes: 0

Rick B
Rick B

Reputation: 11

Your code isn't actually finding the maximum, it's printing out every number that is bigger than the first number it reads in. Notice that you never assign big a new value. Also, maximum is purely keeping track of the numbers larger than big (the first number in the file).

This should do it:

while (fileScanner.hasNextInt()) {
    int num = fileScanner.nextInt();
    if (num > big) 
       big = num;
}

Upvotes: 1

Ratul Bin Tazul
Ratul Bin Tazul

Reputation: 2151

While there are many errors in your code. I think the problem that you asked for is why it is printing multiple numbers.

Just put the System.out.println(num); this line outside of while loop. And fix the other errors to make it work.

Upvotes: 0

Related Questions