0
  • First I create a txt file (a.txt) -- DONE
  • create 10 random number from - to ( like from 5 -10 ) --DONE
  • I write this number in txt file --DONE
  • I want to check its written or not -- DONE
  • Now I need to find: how many number, biggest, smallest, sum of numbers But I can not call that file and search in the file (a.txt). I am just sending last part. Other parts work. I need some help to understand. It is also inside another method. not main

    Scanner keyboard = new Scanner(System.in);
    boolean again = true;
    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    int a = 0;
    int count = 0;
    
    System.out.println("Enter the filename to write into all analysis: ");
    outputFileName = keyboard.nextLine();
    
    File file2 = new File(outputFileName);
    if (file2.exists()) {
        System.out.println("The file " + outputFileName +
            " already exists. Will re-write its content");
    }
    
    
    try {
        PrintWriter yaz = new PrintWriter(file2);
    
        // formulas here. created file a.txt  need to search into that file biggest smallest and sum of numbers
    
        yaz.println("Numeric data file name: " + inputFileName);
        yaz.println("Number of integer: " + numLines);
        yaz.println("The total of all integers in file: " + numLines); //fornow
        yaz.println("The largest integer in the set: " + max);
        yaz.println("The smallest integer in the set " + min);
    
        yaz.close();
        System.out.println("Data written to the file.");
    } catch (Exception e) {
        System.out.printf("ERROR reading from file %s!\n", inputFileName);
        System.out.printf("ERROR Message: %s!\n", e.getMessage());
    
    }
    
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
canntsn
  • 33
  • 1
  • 7

2 Answers2

0

You need to read the file into memory. One way to do that is to move the text of the file into a String.

This post will help you: Reading a plain text file in Java

Here's the relevant code:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    } 
    String everything = sb.toString();
} 
Community
  • 1
  • 1
Glen Pierce
  • 3,247
  • 3
  • 28
  • 44
0

So you want a code to read a text file and give you the biggest, smallest and the average. You can use Scanner class for that and use hasNextInt() to find integers

        File f = new File("F:/some_text_file.txt"); // input your text file here
    if(f.exists()){
        try{    
            Scanner sc = new Scanner(f);
            int max = Integer.MIN_VALUE;
             int min = Integer.MAX_VALUE;
             int temp=0, i=0;
             double sum=0;
            while(sc.hasNextInt()){
                temp = sc.nextInt();
                if(temp>max) max = temp;
                if(temp<min) min =temp;
                sum+=(double) temp;
                i++;

            }
            System.out.println("average : " +sum/i);    
            System.out.println("large : "+max);
            System.out.println("small :"+min);
            sc.close();
        }catch(Exception e){
            e.printStackTrace();
        }       
    }

See if this works

Kangkan
  • 117
  • 1
  • 2
  • 9
  • 1
    thanks, akl3http://stackoverflow.com/users/7895066/kl38453384533. that works.. now I am reading the code. I have to understand. – canntsn Apr 30 '17 at 05:29