Advertisement
Guest User

file

a guest
Apr 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Scanner;// importing all the classes in util package class task
  7.  
  8. public class Assignment3 {
  9.  
  10. public static void main(String[] args) throws FileNotFoundException {
  11. Scanner str = new Scanner(System.in);
  12. System.out.println("Enter the Team Name:");
  13. System.out.println("-->");
  14. String teamName= str.next();
  15. int st=0;
  16.  
  17. //initiation of all the variables
  18. String HomeTeam ="";
  19. String awayTeam="";
  20. int homeScore=0;
  21. int awayScore=0;
  22. String error="";
  23.  
  24. Scanner filename = new Scanner(System.in);//a Scanner object for input is created Scanner Yo = new Scanner(System.in);
  25. System.out.println("enter the file name with file extension:");
  26. System.out.print("-->");
  27. String textfilename= filename.next();
  28.  
  29.  
  30. //bufferreader object for reading file
  31. BufferedReader br = new BufferedReader(new FileReader(textfilename));
  32. //the title of table
  33. System.out.println("HomeTeam\tAwayTeam\tHome-Score\tAway-Score");
  34.  
  35. //variables for scanning a line of string
  36. String readLine = null;
  37. try {
  38. //reading line to the end of file
  39. while((readLine = br.readLine()) != null){
  40. long count = readLine.chars().filter(ch -> ch == ':').count();//count the delimeter
  41. if(count<3) { //if it is less then 3 program is paused
  42.  
  43. error="the match.txt is not a valid file or some charecter is missing from file";
  44. break;
  45. }
  46.  
  47. String [] game = readLine.split(":"); //spliting the line using delimiter
  48. //storing the text data to program variables
  49. HomeTeam=game[0];
  50. if(HomeTeam.length()==0){//if length is 0 it means it is empty ,this program will be halted
  51.  
  52. error="Home team name may be missing ";
  53. break;
  54. }
  55.  
  56. awayTeam=game[1];
  57. if(awayTeam.length()==0){//if length is 0 it means it is empty ,this program will be halted
  58.  
  59. error="Away team name may be missing ";
  60. break;
  61. }
  62. if(game[2].length()==0){//if length is 0 it means it is empty ,this program will be halted
  63.  
  64. error="Home Team Score May be Missing";
  65. break;
  66. }
  67. try{
  68. homeScore=Integer.parseInt(game[2]);//parsing to integer form, if failed while parsing program is exit
  69. }catch(NumberFormatException ex){
  70.  
  71. System.out.println("The Home Team Score is not a valid Integer ");
  72. break;
  73. }
  74. if(game[2].length()==0){//if length is 0 it means it is empty ,this program will be halted
  75.  
  76. error="Away Team Score May be Missing";
  77. break;
  78. }
  79. try{
  80. awayScore=Integer.parseInt(game[3]);//parsing to integer form, if failed while parsing program is exit
  81.  
  82. }catch(NumberFormatException ex){
  83.  
  84. System.out.println("The Away Team Score is not a valid Integer ");
  85. break;
  86. }
  87. //the outputs
  88. if(teamName.equals(HomeTeam)){
  89. System.out.print(HomeTeam);
  90. System.out.println("\t"+"\t"+homeScore);
  91. st=1;
  92. }else if(teamName.equals(awayTeam)){
  93. System.out.print("\t"+"\t"+"\t"+"\t"+awayTeam);
  94. System.out.println("\t"+"\t"+awayScore);
  95. st=1;
  96. }
  97.  
  98.  
  99. }
  100. if(st==0){
  101. clear();
  102. error="No team found!";
  103. }
  104. } catch (IOException ex) {//shows error while reading files
  105.  
  106. }
  107. System.out.println(error); //printing error
  108.  
  109. }
  110. static void clear(){//clear screen
  111. for(int i=0; i<1000;i++){
  112. System.out.println("\n");
  113. }
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement