1

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.

qqq
  • 13
  • 1
  • 4
  • 1
    there is some logical errors in your code. what you want to do is that read a number from the text and file and compare if that is greater than the previous maximum number. if so, update the maximum else check the next number. after reading the entire file, you should then print the value of max variable – amyn Mar 31 '17 at 16:09

5 Answers5

1

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;
}
Rick B
  • 11
  • 2
0

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);
Razib
  • 10,965
  • 11
  • 53
  • 80
0

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.

Ratul Bin Tazul
  • 2,121
  • 1
  • 15
  • 23
0

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.

FilipRistic
  • 2,661
  • 4
  • 22
  • 31
0
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

Community
  • 1
  • 1
jrook
  • 3,459
  • 1
  • 16
  • 33
  • I completely understand this logic but when I try it, its not printing the max, instead the content of the file. What is happening? :( – qqq Mar 31 '17 at 16:25
  • That's because you (and I!) were using the println inside the loop. I fixed my code to only print the final maximum value. I thought you wanted to show every value until it hit the max. Apparently, you are only interested in the maximum value. So the println (or return statement) should be at the end of the code. – jrook Mar 31 '17 at 16:30
  • :O Yes that was it! Thank you! – qqq Mar 31 '17 at 16:35