0

I have file of which I need to read input. On one of the lines, there is no name added. In this case, I want to print out that no match was found. The problem that I'm having is that I don't know how I can make sure the program actually reads the part as an empty string. What happens now is that the will just leave the line empty on the console.

The date input looks like this:

5=20=22=10=2=0=0=1=0=1;Vincent Appel,Johannes Mondriaan

2=30=15=8=4=3=2=0=0=0;

class Administration {

public static final int TOTAL_NUMBER_OF_SIMULARITY_SCORES = 10;
public static final String ZERO_MATCHES = "_";
public static final String LESS_THAN_TWENTY_MATCHES= "-";
public static final String TWENTY_OR_MORE_MATCHES = "^";


PrintStream out;    

Administration() {
    out = new PrintStream(System.out);

}


void printSimilarityScores (Scanner similarityScoresScanner, String similarityScoresInput) {
    similarityScoresScanner.useDelimiter("=|;");
    int length = similarityScoresInput.length();

    for (int i = 0; i < TOTAL_NUMBER_OF_SIMULARITY_SCORES; i++) {
        int grade = similarityScoresScanner.nextInt();

        if (grade == 0) {
            out.printf(ZERO_MATCHES);
        } else if (grade < 20) { 
            out.printf(LESS_THAN_TWENTY_MATCHES);
        } else {
            out.printf(TWENTY_OR_MORE_MATCHES);
        }

    } 

    System.out.print("\n");
    similarityScoresScanner.useDelimiter(";|,");

    while(similarityScoresScanner.hasNext()) {
        String name = similarityScoresScanner.next();

        if (length < 22) {
            out.printf("No matches found\n");
        } else {
            System.out.print("\n" + name);
        }

    } 

} 




void start() {

    Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner();    

        while (fileScanner.hasNext()) { 

            String finalGradeInput = fileScanner.nextLine();
            String similarityScoresInput = fileScanner.nextLine();

            Scanner finalGradeInputScanner = new Scanner(finalGradeInput);
            Scanner similarityScoresScanner = new Scanner(similarityScoresInput);

            printFinalGrade(finalGradeInputScanner);
            printSimilarityScores(similarityScoresScanner, similarityScoresInput);

        }
    }

public static void main(String[] argv) {

    new Administration().start();

}

}

0

1 Answer 1

1

An easier solution would be to read the file, line by line and handle them like this :

  • split by the separator
  • check if there is more than 1 element,
  • if positive print them
Scanner similarityScoresScanner = new Scanner(myFile);
while (similarityScoresScanner.hasNextLine()) {
    String[] content = similarityScoresScanner.nextLine().split("[;,]");
    if (content.length == 1) {
        System.out.println("No matches found");
    } else {
        for (int i = 1; i < content.length; i++) {
            System.out.println(content[i]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for helping! Would there be a way to do this without an array?
@Zsa_Sza of course, but it won’t be nice, like manipulating the line using indexof comma and substring, the solution I show you is the one I would use, always read line by line and handle them
Alright the thing is, that I can't use arrays in this particular case. I've adjusted the code above. The issue I'm getting now is that the length doesn't match when I count inside the while loop. I you are willing to have another look that would be great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.