0
public static void studentNumber() throws IOException
{
    int word2;
    int val = 0;
    System.out.println("Enter the Student Number");
    word2 = sc2.nextInt();
    Scanner file = new Scanner(new File("data.txt"));
    System.out.println();
    while(file.hasNextLine())           
    {
        String line = file.nextLine();
        if(line.indexOf(word2) != -1)
        {
            System.out.println("The student" +word2+ " exists in our system, their phone number is");
            val = 1;
            break;
        }
        else
        {
            val = 0;
            continue;
        }
    }
    if(val == 0)
    {
        System.out.println("The student " +word2+ " does not exist in our system");
    }                
}   
}
  1. Allow the user to search for student data according to

    • a. Surname and First Name
    • b. Student number
    • If the student exists: display their phone number(s) along with the name
    • If the student does not exist, alert the user to this fact.

Here is an example of the list from my text file:

Barrett,Zulema,848284,M,3A1,3H2,S3,961025,2891374756, ,CHT3O0ACLU3M0BENG3C0EHIR3C0AMBF3C0CPPL3OMAPPL4OMCSBI3C0B

duki boy
  • 1
  • 4

2 Answers2

0

There are some possibly undesirable behaviours associated with the section of code below:

   if(line.indexOf(word2) != -1)
   {
      System.out.println("The student" +word1+ "," +word2 +"exists in our system, their phone number is" );
      val = 1;
      break;
   }
  1. The println command does not attempt to output the found student number and there is no code to extract this from the line. These are the primary reasons this code does not output the student number. Assign a variable to the student number and include it in the output statement. A String split (see point #3 below) could be used to extract this information.

  2. The if test is only checking the condition that word2 exists in the string but the text output implies that both word1 and word2 were present in the tested line. (This logic would also be easier to read of you used the surname and first_name variables.)

  3. Checking if a string is present in the line using indexOf will return false positives for any substring match including partial strings at any position. e.g. Searching for the surname "Adams" will also match "Adamson". If you split the string using the comma delimiter and assign meaningful names to each element you will be able to discriminate between which part of the record is matched. See this thread for ideas on Java String splitting

  4. Including a break after each found line will mean only 1 match is ever found. Outputting matched data or adding found data to a collection will avoid missed items.

Addressing these 4 points should improve the chance that searchStudentName() will have the desired behaviour and these could be applied to studentNumber() too.

Other general observations that would improve this code are:

  • Use a meaningful name and strict type for determining whether a match was found substituting "int val" for something like "boolean foundMatchingRecord".

  • Create a light java object to hold all the attributes from each record against meaningful name and add rich interrogations methods such as "matchesFirstNameAndSurname()"

  • Handle the IOException when you can still discriminate between a user console input problem and a file read problem to add a useful failure message and exit status.

  • create a POJO for all the logic and a Unit test to test it rather than a set of static methods in the same class as the main() method.

Community
  • 1
  • 1
antonycc
  • 76
  • 1
  • 4
  • for if state ment can i do this if(line.indexOf(word1) != -1 && line.indexOf(word2) != -1) – duki boy Dec 07 '14 at 16:09
  • i still don't get split? – duki boy Dec 07 '14 at 16:15
  • That looks like it would compile and it would check for both the presence of word1 and word2 but still at any arbitrary position in the record. An example of undesirable behaviour associated with this is that if a user entered "Barrett" for both the first name and surname, the first line in your example data would still match even though the first name is "Zulema". – antonycc Dec 07 '14 at 16:17
  • @ so instead of && do ||? – duki boy Dec 07 '14 at 16:18
  • Split takes a string like "A,B,C" and a delimiter (comma) converts this into an array containing just the letters so you can access each value individually. See [String#split](http://docs.oracle.com/javase/8/docs/api/index.html?java/lang/String.html} – antonycc Dec 07 '14 at 16:20
  • for the split can i do this? String textStr[] = data.split(","); – duki boy Dec 07 '14 at 16:20
  • || would be even less strict. Extract the record for the surname and firstname then use if(candidateSurname.equals(surname) && candidateFirstName.equals(first_name)). – antonycc Dec 07 '14 at 16:22
  • what do u mean extract the record, idk how to do that and do u mean like extract row 0 and row 1? – duki boy Dec 07 '14 at 16:24
  • so i ca do this candidate Surname=data[0]; and candidatFirstName=data[1];? – duki boy Dec 07 '14 at 16:41
  • so it would be String text[]= line.split("");? for candidate surname/firstname it would be String candidateSurname = line[0]; and String candidateFirstName=line[1]; – duki boy Dec 07 '14 at 17:11
  • can i do this Scanner scanner = new Scanner(InputStream);//Get File Input stream here StringBuilder builder = new StringBuilder(); while (scanner.hasNextLine()) { builder.append(scanner.nextLine()); builder.append(" ");//Additional empty space needs to be added } String strings[] = builder.toString().split(" "); System.out.println(Arrays.toString(strings)); – duki boy Dec 07 '14 at 17:17
  • idk can u please help me? – duki boy Dec 07 '14 at 17:18
  • I have moved this to an additional answer. Also, a correction, the delimiter passed to split is a comma not an apostrophe, it should be: line.split(",") – antonycc Dec 07 '14 at 17:37
0

Here is an example of using String.split() to break a record into individual fields:

  public static class Student{
        public String firstName;
        public String surname;
        public int number;
        public String field04;
        public String field05;
        public String field06;
        public String field07;
        public String field08;
        public String field09;
        public String field10;
        public String field11;
    }

   @Test
   public void expectToFindAMatchForFirstNameAndSurname() {

      // Test parameters
      String record = "Barrett,Zulema,848284,M,3A1,3H2,S3,961025,2891374756, ,CHT3O0ACLU3M0BENG3C0EHIR3C0AMBF3C0CPPL3OMAPPL4OMCSBI3C";
        String firstNameToFind = "Zulema";
        String surnameToFind = "Barrett";

        // Split String
       String[] fields = record.split(",");
       Student student = new Student();
       if (fields.length >= 1) student.surname = fields[0];
       if (fields.length >= 2) student.firstName = fields[1];
       if (fields.length >= 3) student.number = Integer.parseInt(fields[2]);
       if (fields.length >= 4) student.field04 = fields[3];
       if (fields.length >= 5) student.field05 = fields[4];
       if (fields.length >= 6) student.field06 = fields[5];
       if (fields.length >= 7) student.field07 = fields[6];
       if (fields.length >= 8) student.field08 = fields[7];
       if (fields.length >= 9) student.field09 = fields[8];
       if (fields.length >= 10) student.field10 = fields[9];
       if (fields.length >= 11) student.field11 = fields[10];

       // Check with names the correct way round
       Assert.assertTrue(firstNameToFind.equals(student.firstName));
       Assert.assertTrue(surnameToFind.equals(student.surname));
       Assert.assertTrue(firstNameToFind.equals(student.firstName) && surnameToFind.equals(student.surname));
       Assert.assertTrue(record.indexOf(firstNameToFind) != -1 && record.indexOf(surnameToFind) != -1);

       // Check with first name and surname reversed
       Assert.assertFalse(firstNameToFind.equals(student.surname));
       Assert.assertFalse(surnameToFind.equals(student.firstName));
       Assert.assertFalse(firstNameToFind.equals(student.surname) && surnameToFind.equals(student.firstName));
       Assert.assertTrue(record.indexOf(surnameToFind) != -1 && record.indexOf(firstNameToFind) != -1);
   }

In this example note how with the firstname and surname swapped round the indexOf against the whole line still finds a match.

antonycc
  • 76
  • 1
  • 4
  • antonycc ik u took time to do this but is there an easy way like less code because i have 1000 students and also u know these "CHT3O0ACLU3M0BENG3C0EHIR3C0AMBF3C0CPPL3OMAPPL4OMCSBI3C", this is course code they are 7 characters each? And i have a list of total of 100 students? – duki boy Dec 07 '14 at 17:44
  • i updated my code can u check it and trying doing something with my code? – duki boy Dec 07 '14 at 18:22