Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1.  
  2. /*Prashanna  Nepal
  3.  *
  4.  *
  5.  *
  6.  *
  7.  *
  8.  *
  9.  *
  10.  */
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.util.Scanner;
  14.  
  15. public class assign {
  16.  
  17.     public static void main(String[] args) {
  18.         // TODO Auto-generated method stub
  19.         String line = "";
  20.         boolean team = true;
  21.         Scanner object = new Scanner(System.in);
  22.         System.out.println("Enter the file name: ");
  23.         String filename = object.nextLine();
  24.         Scanner fileReader = null;
  25.         File Fileobject = new File(filename);
  26.         int invalid = 0;
  27.  
  28.         if (!Fileobject.exists()) {
  29.             System.out.println("file missing ");
  30.             System.exit(0);
  31.         }
  32.         try {
  33.  
  34.             fileReader = new Scanner(Fileobject);// reading the file user wants
  35.             System.out.println("\nThe file " + filename + "\n" + "conatins the following data");
  36.             System.out.println("\n======================");
  37.             System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "Home team", "Score", "Away team", "Score"); // printing
  38.                                                                                                             // these in
  39.                                                                                                             // console
  40.             System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "=========", "=====", "=========", "=====");
  41.  
  42.             while (fileReader.hasNext()) {
  43.                 line = fileReader.nextLine();
  44.                 String[] splitText = line.split(":");
  45.                 // in case of delimeter is missing
  46.                 int delimetercount = 0;// setting delimeter counter
  47.                 String[] delimeter = line.split("");
  48.                 for (String dd : delimeter) {
  49.                     if (dd.equals(":")) {
  50.                         delimetercount++;
  51.  
  52.                     }
  53.                 }
  54.                 if (delimetercount != 3)// checking if delimeter equals to 3
  55.                 {
  56.  
  57.                     ++invalid;
  58.                     System.out.println("");
  59.                     System.out.printf("\n%-35s", "Delimeter missing or wrong delimeter may be used in this line\n ");
  60.                     continue;
  61.  
  62.                 }
  63.  
  64.                 if (splitText.length == 4)// making sure there is 4 part in array
  65.                 {
  66.                     splitText[0] = splitText[0].trim();// removing unwanted spaces
  67.                     splitText[1] = splitText[1].trim();
  68.                     splitText[2] = splitText[2].trim();
  69.                     splitText[3] = splitText[3].trim();
  70.  
  71.                 }
  72.                 if (!splitText[0].equals(""))// conditions if split array is not equal to blank
  73.                 {
  74.                     String homet = splitText[0];
  75.                     System.out.printf("\n" + "%-28s", homet);
  76.  
  77.                 } else {
  78.                     System.out.printf("\n%-28s", "HOME NAME IS MISSING");
  79.  
  80.                     team = false;
  81.                 }
  82.  
  83.                 if (!splitText[2].equals("")) {
  84.                     try {
  85.                         int score = Integer.parseInt(splitText[2]);
  86.                         int hscore = score;
  87.                         System.out.printf("" + "%-22s", hscore);
  88.                     } catch (NumberFormatException e) {
  89.  
  90.                         System.out.printf("%-22s", "NON NUMERIC DATA");// printing this in console
  91.                         team = false;// validating for invalid entries
  92.                     }
  93.                 } else {
  94.  
  95.                     System.out.printf("%-22s", "SCORE IS MISSING");// printing this in console
  96.                     team = false;// validating for invalid entries
  97.  
  98.                 }
  99.  
  100.                 if (!splitText[1].equals("")) {
  101.                     String awayt = splitText[1];
  102.                     System.out.printf("" + "%-27s", awayt);
  103.                 } else {
  104.  
  105.                     System.out.printf("%-27s", "AWAY NAME IS MISSING");
  106.                     team = false;
  107.                 }
  108.  
  109.                 if (!splitText[3].equals("")) {
  110.                     try {
  111.                         int atscore = Integer.parseInt(splitText[3]);
  112.                         int ascore = atscore;
  113.                         System.out.printf("" + "%-25s", ascore);
  114.                     } catch (NumberFormatException e) {
  115.  
  116.                         System.out.printf("%-25s", "NON NUMERIC DATA");// printing this in console
  117.                         team = false;// validating for invalid entries
  118.                     }
  119.                 } else {
  120.  
  121.                     System.out.printf("%-25s", "SCORE IS MISSING");// printing this in console
  122.                     team = false;// validating for invalid entries
  123.  
  124.                 }
  125.                 if (team == false) {
  126.  
  127.                     ++invalid;// increasing invalid counter
  128.  
  129.                 }
  130.                 team = true;// reseting the boolean value
  131.             }
  132.  
  133.         } // end of try block
  134.         catch (FileNotFoundException e) {
  135.             System.out.println("Error - File does not exist");
  136.         }
  137.         System.out.println("\n-----------------");
  138.         // counting total number of invalid books
  139.         if (invalid == 0 || invalid == 1) {
  140.             System.out.println("\nTotal number of invalid entries: " + invalid);
  141.         } else {
  142.             System.out.println("\nTotal number of invalid entries: " + invalid);
  143.         }
  144.  
  145.         // searching
  146.         try {
  147.             fileReader = new Scanner(Fileobject);// reading the file user wants
  148.             Scanner f = new Scanner(System.in);// making Scanner class for taking in team name
  149.             System.out.println("");
  150.             System.out.println("Enter the title of the book you want to search ");
  151.             String title = f.nextLine();// taking the team name user wants to enter
  152.             System.out.println("\nThe file " + filename + "\n" + "conatins the following data");
  153.             System.out.println("\n======================");
  154.             System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "Home team", "Score", "Away team", "Score"); // printing
  155.                                                                                                             // these in
  156.                                                                                                             // console
  157.             System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "=========", "=====", "=========", "=====");
  158.  
  159.             while (fileReader.hasNext()) {
  160.  
  161.                 String line1 = fileReader.nextLine();// Read a line of data from text file
  162.                 String[] arraysplit = line1.split(":");
  163.                 if (arraysplit.length == 4)// making sure there is 4 part of the array
  164.                 {
  165.                     arraysplit[0] = arraysplit[0].trim();
  166.                     arraysplit[1] = arraysplit[1].trim();
  167.                     arraysplit[2] = arraysplit[2].trim();
  168.                     arraysplit[3] = arraysplit[3].trim();
  169.  
  170.                     try {
  171.  
  172.                         if (!arraysplit[0].equals("") && !arraysplit[1].equals("") && arraysplit[2].length() > 0
  173.                                 && arraysplit[3].length() > 0
  174.                                 && (title.equals(arraysplit[0]) || title.equals(arraysplit[1])))
  175.                         // putting the valid condition and enter title for searching//validating
  176.                         {
  177.                             String homen = arraysplit[0];
  178.                             String awayn = arraysplit[1];
  179.                             int score = Integer.parseInt(arraysplit[2]);
  180.                             int htscore = score;
  181.                             int atscore = Integer.parseInt(arraysplit[3]);
  182.                             int ascore = atscore;
  183.                             System.out.printf("\n" + "%-28s", homen);
  184.                             System.out.printf("" + "%-22s", htscore);
  185.                             System.out.printf("" + "%-27s", awayn);
  186.                             System.out.printf("" + "%-25s", ascore);
  187.                         }
  188.                     }
  189.                     // try close
  190.                     catch (Exception e) {
  191.  
  192.                     }
  193.                 }
  194.  
  195.             }
  196.  
  197.             System.out.println("\n-------------------------------------");
  198.             System.out.println("Sorry there is no team having that name\n or there is no more data to display!! ");
  199.             //printing  if  team  is unavailable or there is no more data left to print
  200.            
  201.            
  202.            
  203.            
  204.         System.out.println();
  205.         } catch (Exception e) {
  206.             System.out.println("Error while reading from file ");
  207.         }
  208.  
  209.         // end of program
  210.  
  211.     }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement