Hey,
I have an assignment to do. I have to read a text file in using the scanner in java, getting it to read the football scores within the file and split them into 4 parts. So basically the files has Home team : away team : hometeam score : away team score. on one line and has multiple lines for multiple games.
I then have to re-arrange them so it displays them neatly. I also have to do the score count. I have done all this above (yay) but im really struggling with having it count if a line is valid. I have so far done it so it counts the string for each segment and if it contains <=0 characters it doesn't display the match but i need to count how many matches are invaild and how many are vaild. For example one line might have a missing score or a missing team name.
I hope this makes sense!
Here is my working code so far:
Code:
I have an assignment to do. I have to read a text file in using the scanner in java, getting it to read the football scores within the file and split them into 4 parts. So basically the files has Home team : away team : hometeam score : away team score. on one line and has multiple lines for multiple games.
I then have to re-arrange them so it displays them neatly. I also have to do the score count. I have done all this above (yay) but im really struggling with having it count if a line is valid. I have so far done it so it counts the string for each segment and if it contains <=0 characters it doesn't display the match but i need to count how many matches are invaild and how many are vaild. For example one line might have a missing score or a missing team name.
I hope this makes sense!
Here is my working code so far:
Code:
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class roaroraors {
public static void main(String[] args) throws FileNotFoundException {
int goals = 0;
int hometeamLength;
int awayteamLength;
int homescoreLength;
int awayscoreLength; //int for away score string count
Scanner s = new Scanner(new File("Demo.txt")); // create a scanner which scans from a file
String line; // stores the each line of text read from the file
while ( s.hasNext() ) {
line = s.nextLine(); // read the next line of text from the file
String [] splitupText = line.split(":"); // split the text into multiple elements
String hometeam = splitupText [0];
String awayteam = splitupText [1];
String homescore = splitupText [2];
String awayscore = splitupText [3];
hometeamLength= hometeam.length();
awayteamLength= awayteam.length();
homescoreLength= homescore.length();
awayscoreLength= awayscore.length();
hometeam = hometeam.trim();
awayteam = awayteam.trim();
homescore = homescore.trim();
awayscore = awayscore.trim();
//System.out.println(hometeam + " [" + homescore+"] "+ "| " +awayteam+" ["+awayscore+"] ");
try { // "try" is a special statement which allows us to deal with "exceptions"
int homescoreint = Integer.parseInt(homescore); // attempt to convert the String into an Integer type value
int awayscoreint = Integer.parseInt(awayscore); // attempt to convert the String into an Integer type value
goals = goals + homescoreint + awayscoreint;
}
catch (NumberFormatException e) {
// The number did not parse for some reason
System.out.println("Invalid number entered"); //print line for invaild number entered
}
if(hometeamLength > 0 && awayteamLength > 0 && homescoreLength >0 && awayscoreLength >0) //if ALL team/score are greater than 0
System.out.println(hometeam + " [" + homescore+"] "+ "| " +awayteam+" ["+awayscore+"] "); //does this if it is greater than 0
else
System.out.println("TEAM or SCORE IS INVAILD"); //does this if it is not greater than or equal to 0
}
System.out.println("\nGoals = " + goals); // Output and End Of File message.
System.out.println("\nVaild Match Count = " ); // Output and End Of File message.
System.out.println("\nInvaild Match Count = " ); // Output and End Of File message.
}
}