Given the task of finding the largest number. There is a file chisla.txt , containing the numbers 3, 8, 1, 5 in the column. It is necessary in the code to read this file and find the largest number.

I know how to display these numbers on the screen and how to write an array to find the largest one in it.
But how to combine these two tasks? How to write in the code this "transition" from one task to another?

 import java.io.*; import java.util.*; public class X { public static void main(String[] args) { BufferedReader in = new BufferedReader(new FileReader("\\chisla.txt")); //как их связать? int[] array = {...}; int max = 0; for (int i = 0; i < array.length; i++) { if (max < array[i]) max = array[i]; } System.out.println("Max: " + max); } } 
  • So did you learn how to work with BufferedReader ? - Regent
  • The teacher taught us this way: he told a story, gave a couple of simple examples that we had to make out on our own and sent us to a free ship (they say, further on). - Konstantin

3 answers 3

Since we started to give answers, you can do this using Java 8:

 public static void main(String[] args) { int[] array = null; try (BufferedReader in = new BufferedReader(new FileReader("chisla.txt"))) { array = in.lines().mapToInt(Integer::parseInt).toArray(); } catch (IOException | NumberFormatException e) { e.printStackTrace(); } if (array != null) { int max = Integer.MIN_VALUE; for (int i = 0; i < array.length; i++) { if (max < array[i]) { max = array[i]; } } System.out.println("Max: " + max); } } 

All strings from the file, or rather the stream of Stream<String> , returned in.lines() , lead to the stream of int numbers ( IntStream , returned by mapToInt ) using Integer.parseInt , then convert this stream to an array int[] with using toArray() .

You also need to remember to close the BufferedReader after reading the data. In the presented code, this is done using the try-with-resources construct.

In general, the maximum can be found using the IntStream itself:

 public static void main(String[] args) { try (BufferedReader in = new BufferedReader(new FileReader("chisla.txt"))) { OptionalInt optionalMax = in.lines().mapToInt(Integer::parseInt).max(); if (optionalMax.isPresent()) { System.out.println("Max: " + optionalMax.getAsInt()); } } catch (IOException | NumberFormatException e) { e.printStackTrace(); } } 
  • Thank you very much! !))) - Konstantin

It is not necessary to throw into the array, you can just read the file line by line, comparing with the current maximum:

 public static void main(String[] args) throws FileNotFoundException, IOException { BufferedReader in = new BufferedReader(new FileReader("\\chisla.txt")); String text; int max = Integer.MIN_VALUE; while ((text = in.readLine()) != null) { // читаем файл построчно if (Integer.parseInt(text) > max) // ищем максимум max = Integer.parseInt(text); } System.out.println("Max: " + max); in.close(); } 
  • @Regent thanks, corrected the answer - Denis
  • I think it is also worth considering that if an exception occurs in 5-7 lines, in.close() will not be called, which is not good. And a minor point in optimization: it would be good for Integer.parseInt be called only once for each row. - Regent

You can use a scanner

 Scanner scanner = new Scanner(new File("tall.txt")); int [] array= new int [100]; int i = 0; while(scanner.hasNextInt()){ array[i++] = scanner.nextInt(); } 

and your method

 BufferedReader in = new BufferedReader(new FileReader("\\chisla.txt")); int [] array= new int [100]; String s =in.readLine(); int i = 0; while(s!=null) { try { array[i++] = Integer.parseInt(s); } catch (NumberFormatException ex) { continue; } s = in.readLine(); } in.close(); 
  • That would be “great” if the file contains more than 100 numbers ... - Regent
  • This is his example, and you need to use ArrayList - Parviz Rozikov
  • "by his example" it is possible and int[4] even then. Do you think it is worthwhile to teach people a bad approach, because "it will come down anyway"? Well, how about the author, in fact, not 4 numbers? Or will future visitors have more than 100 numbers? And it is not necessary to use the list - it is possible and an array. - Regent