Advertisement
Guest User

Untitled

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