-1

I am a noobie at programming and I can't seem to figure out what to do.

I am to write a Java program that reads in any number of lines from a file and generate a report with:

the count of the number of values read the total sum the average score (to 2 decimal places) the maximum value along with the corresponding name. the minimum value along with the corresponding name.

The input file looks like this:

55527    levaaj01   
57508    levaaj02   
58537    schrsd01   
59552    waterj01   
60552    boersm01   
61552    kercvj01   
62552    buttkp02   
64552    duncdj01   
65552    beingm01   

I program runs fine, but when I add in score = input.nextInt(); and player = input.next(); The program stops working and the keyboard input seems to stop working for the filename. I am trying to read each line with the int and name separately so that I can process the data and complete my assignment. I don't really know what to do next.

Here is my code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class Program1 {

private Scanner input = new Scanner(System.in);
private static int fileRead = 0;
private String fileName = "";
private int count = 0;
private int score = 0;
private String player = "";

public static void main(String[] args) {

    Program1 p1 = new Program1();

    p1.getFirstDecision();

    p1.readIn();

}  

public void getFirstDecision() { //*************************************

    System.out.println("What is the name of the input file?");

    fileName = input.nextLine(); // gcgc_dat.txt

}



public void readIn(){           //*********************************************

    try {

        FileReader fr = new FileReader(fileName + ".txt");
        fileRead = 1;
        BufferedReader br = new BufferedReader(fr); 

        String str;

        int line = 0;

        while((str = br.readLine()) != null){

           score = input.nextInt();
           player = input.next();

           System.out.println(str);  

           line++;
           score = score + score;
           count++;

        }

       System.out.println(count);
       System.out.println(score);             

       br.close();

    }

    catch (Exception ex){

       System.out.println("There is no shop named: " + fileName);         

    }    

  } 

}
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Xerias91
  • 3
  • 1
  • 1
    To compare strings you need to use .equals() not ==. – ruthless Sep 04 '14 at 22:55
  • I think your assignment is steering you towards putting the data you read into a list- or dictionary-like structure to perform the reporting on - create the aggregates (sum + avg) and sort ascending and descending (min + max). – Filburt Sep 04 '14 at 23:08
  • 1
    @DavidPostill on top. the code is very confusing. the op using BufferReader to read this file but use scanner to read variables?!!!! – Kick Buttowski Sep 04 '14 at 23:11
  • @xerias91 I post my answer lemme know how much I helped – Kick Buttowski Sep 05 '14 at 05:19

1 Answers1

1

The way you used BufferReader with Scanner is totally wrong .

Note: you can use BufferReader in Scanner constructor.

For example :

try( Scanner input = new Scanner( new BufferedReader(new FileReader("your file path goes here")))){

 }catch(IOException e){
 }

Note: your file reading process or other processes must be in try block because in catch block you cannot do anything because your connection is closed. It is called try catch block with resources.

Note:

A BufferedReader will create a buffer. This should result in faster reading from the file. Why? Because the buffer gets filled with the contents of the file. So, you put a bigger chunk of the file in RAM (if you are dealing with small files, the buffer can contain the whole file). Now if the Scanner wants to read two bytes, it can read two bytes from the buffer, instead of having to ask for two bytes to the hard drive.

Generally speaking, it is much faster to read 10 times 4096 bytes instead of 4096 times 10 bytes.

Source BufferedReader in Scanner's constructor

Suggestion: you can just read each line of your file by using BufferReader and do your parsing by yourself, or you can use Scanner class that gives you ability to do parsing tokens.

difference between Scanner and BufferReader

As a hint you can use this sample for your parsing goal

Code:

        String input = "Kick 20";
        String[] inputSplited = input.split(" ");
        System.out.println("My splited name is " + inputSplited[0]);
        System.out.println("Next year I am " + (Integer.parseInt(inputSplited[1])+1));

Output:

My splited name is Kick
Next year I am 21

Hope you can fixed your program by given hints.

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58