Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Prashanna Nepal
- *
- *
- *
- *
- *
- *
- *
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class assign {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String line = "";
- boolean team = true;
- Scanner object = new Scanner(System.in);
- System.out.println("Enter the file name: ");
- String filename = object.nextLine();
- Scanner fileReader = null;
- File Fileobject = new File(filename);
- int invalid = 0;
- if (!Fileobject.exists()) {
- System.out.println("file missing ");
- System.exit(0);
- }
- try {
- fileReader = new Scanner(Fileobject);// reading the file user wants
- System.out.println("\nThe file " + filename + "\n" + "conatins the following data");
- System.out.println("\n======================");
- System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "Home team", "Score", "Away team", "Score"); // printing
- // these in
- // console
- System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "=========", "=====", "=========", "=====");
- while (fileReader.hasNext()) {
- line = fileReader.nextLine();
- String[] splitText = line.split(":");
- // in case of delimeter is missing
- int delimetercount = 0;// setting delimeter counter
- String[] delimeter = line.split("");
- for (String dd : delimeter) {
- if (dd.equals(":")) {
- delimetercount++;
- }
- }
- if (delimetercount != 3)// checking if delimeter equals to 3
- {
- ++invalid;
- System.out.println("");
- System.out.printf("\n%-35s", "Delimeter missing or wrong delimeter may be used in this line\n ");
- continue;
- }
- if (splitText.length == 4)// making sure there is 4 part in array
- {
- splitText[0] = splitText[0].trim();// removing unwanted spaces
- splitText[1] = splitText[1].trim();
- splitText[2] = splitText[2].trim();
- splitText[3] = splitText[3].trim();
- }
- if (!splitText[0].equals(""))// conditions if split array is not equal to blank
- {
- String homet = splitText[0];
- System.out.printf("\n" + "%-28s", homet);
- } else {
- System.out.printf("\n%-28s", "HOME NAME IS MISSING");
- team = false;
- }
- if (!splitText[2].equals("")) {
- try {
- int score = Integer.parseInt(splitText[2]);
- int hscore = score;
- System.out.printf("" + "%-22s", hscore);
- } catch (NumberFormatException e) {
- System.out.printf("%-22s", "NON NUMERIC DATA");// printing this in console
- team = false;// validating for invalid entries
- }
- } else {
- System.out.printf("%-22s", "SCORE IS MISSING");// printing this in console
- team = false;// validating for invalid entries
- }
- if (!splitText[1].equals("")) {
- String awayt = splitText[1];
- System.out.printf("" + "%-27s", awayt);
- } else {
- System.out.printf("%-27s", "AWAY NAME IS MISSING");
- team = false;
- }
- if (!splitText[3].equals("")) {
- try {
- int atscore = Integer.parseInt(splitText[3]);
- int ascore = atscore;
- System.out.printf("" + "%-25s", ascore);
- } catch (NumberFormatException e) {
- System.out.printf("%-25s", "NON NUMERIC DATA");// printing this in console
- team = false;// validating for invalid entries
- }
- } else {
- System.out.printf("%-25s", "SCORE IS MISSING");// printing this in console
- team = false;// validating for invalid entries
- }
- if (team == false) {
- ++invalid;// increasing invalid counter
- }
- team = true;// reseting the boolean value
- }
- } // end of try block
- catch (FileNotFoundException e) {
- System.out.println("Error - File does not exist");
- }
- System.out.println("\n-----------------");
- // counting total number of invalid books
- if (invalid == 0 || invalid == 1) {
- System.out.println("\nTotal number of invalid entries: " + invalid);
- } else {
- System.out.println("\nTotal number of invalid entries: " + invalid);
- }
- // searching
- try {
- fileReader = new Scanner(Fileobject);// reading the file user wants
- Scanner f = new Scanner(System.in);// making Scanner class for taking in team name
- System.out.println("");
- System.out.println("Enter the title of the book you want to search ");
- String title = f.nextLine();// taking the team name user wants to enter
- System.out.println("\nThe file " + filename + "\n" + "conatins the following data");
- System.out.println("\n======================");
- System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "Home team", "Score", "Away team", "Score"); // printing
- // these in
- // console
- System.out.printf("\n" + "%-26s%-24s%-25s%-25s ", "=========", "=====", "=========", "=====");
- while (fileReader.hasNext()) {
- String line1 = fileReader.nextLine();// Read a line of data from text file
- String[] arraysplit = line1.split(":");
- if (arraysplit.length == 4)// making sure there is 4 part of the array
- {
- arraysplit[0] = arraysplit[0].trim();
- arraysplit[1] = arraysplit[1].trim();
- arraysplit[2] = arraysplit[2].trim();
- arraysplit[3] = arraysplit[3].trim();
- try {
- if (!arraysplit[0].equals("") && !arraysplit[1].equals("") && arraysplit[2].length() > 0
- && arraysplit[3].length() > 0
- && (title.equals(arraysplit[0]) || title.equals(arraysplit[1])))
- // putting the valid condition and enter title for searching//validating
- {
- String homen = arraysplit[0];
- String awayn = arraysplit[1];
- int score = Integer.parseInt(arraysplit[2]);
- int htscore = score;
- int atscore = Integer.parseInt(arraysplit[3]);
- int ascore = atscore;
- System.out.printf("\n" + "%-28s", homen);
- System.out.printf("" + "%-22s", htscore);
- System.out.printf("" + "%-27s", awayn);
- System.out.printf("" + "%-25s", ascore);
- }
- }
- // try close
- catch (Exception e) {
- }
- }
- }
- System.out.println("\n-------------------------------------");
- System.out.println("Sorry there is no team having that name\n or there is no more data to display!! ");
- //printing if team is unavailable or there is no more data left to print
- System.out.println();
- } catch (Exception e) {
- System.out.println("Error while reading from file ");
- }
- // end of program
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement