This discussion is archived
6 Replies Latest reply: Apr 8, 2013 10:29 AM by thomas.behr RSS

function- boolean

1001484 Newbie
Currently Being Moderated
hi, i've tried to solve my coding but its still have an error at line 153. the question ask me to display the student who got the college. and it use the boolean data type.

import Student.Student;     //student yang pertama nama folder yg kedua nama class
import java.io.*; //important for io file.
import java.util.Scanner;
import java.util.StringTokenizer;

public class StudIOFileApp{
     public static void main (String [] args) throws IOException, Exception{ //exceptional handler- untuk kenal pasti bahawa  file yg dicipta itu sudah wujud atau belum
          try {
               
               Scanner scan = new Scanner (System.in);
               String lineSeparator = System.getProperty("line.separator");
               scan.useDelimiter(lineSeparator);
          
               //instantiation
               Student[] stud = new Student[100];
               
               //HAFAL LINE 18- 31
               BufferedReader StudInfile = new BufferedReader (new FileReader("Student.txt"));
               String records = null;
               int cntRecords = 0;
               
               while((records = StudInfile.readLine()) != null){
                    StringTokenizer token = new StringTokenizer (records, ";");
                    String studID = token.nextToken();
                    String studName = token.nextToken();
                    String studProg = token.nextToken();
                    String studGender = token.nextToken();
                    int studAge = Integer.parseInt(token.nextToken()); //UBAH DATA STRING KEPADA YANG BERKAITAN
                    boolean studCollege = Boolean.parseBoolean(token.nextToken());
                    //stud[cntRecords] = new Student (studID, studName, studProg, studGender, studAge); //normal constructor
                    stud[cntRecords] = new Student (studID, studName, studProg, studGender, studAge, studCollege); //normal constructor
                    cntRecords++;
                    
                    }     
                    
               //display of student profile
                    //Question1 (stud,cntRecords);
               
               //display student ID and name only
                    //Question2 (stud,cntRecords);
                    
               //display all student from CS220
                    //Question3 (stud,cntRecords);
                    
               //count total CS220 student
                    //Question4 (stud,cntRecords);
                    
               //change the program for a student from CS220 to CS 225
                    //Question5 (stud,cntRecords);
               
               //Display total fee collected for Cs220
                    //Question6 (stud,cntRecords);
                    
               //write all student from CS220 into a text file named StudentCS220.txt
                    //Question7 (stud,cntRecords);
                    
               //display all student which are offered college
                    Question8 (stud,cntRecords);
                    
               //count male and female student seperately
                    //Question9 (stud,cntRecords);
                    
               //write all male student in a textfile named studentmale.txt
                    //Question10 (stud,cntRecords);
                    
               //display back the contents of array
                    /*     for (int i = 0; i < cntRecords; i++)
                    {
                         System.out.println(stud.toString() ); //display printer //to string untuk display semua sekali.
               
                    } */
          
          
          }//close try
          
          catch (IOException ice){
               
               System.out.println (ice.getMessage()); //to display type of error
          }
          
          catch (Exception e){
               
               System.out.println (e.getMessage()); //to display type of error
          }
          
          finally{
               
               System.out.println("program end here...");
               }
          }
     //closs main
     
     
          public static void Question10(Student[] stud, int cntRecords)throws IOException, Exception
     {
          try
          {
               PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter("MaleStudentCS220.txt",false)));
               for(int i = 0; i < cntRecords; i++)
          {
               if (stud[i].getStudGender() .equals ("MALE"))
                    outfile.print(stud[i].toString());
               }
          
     
               outfile.close();
          }
          
          catch (IOException ice){
               
               System.out.println (ice.getMessage()); //to display type of error
          }
          
          catch (Exception e){
               
               System.out.println (e.getMessage()); //to display type of error
          }
          
          finally{
               
               System.out.println("program end here...");
               }
          
          
     }
          public static void Question9(Student[] stud, int cntRecords)
     {
          int count = 0, count1 = 0;;
          
          for(int i = 0; i < cntRecords; i++)
          {     
               if (stud[i].getStudGender() .equals ("MALE"))
                    count++;
               else
                    count1++;          
          }
          System.out.println("The total of female student is "+count1 );
          System.out.println("The total of male student is "+count );
     }
     
     public static void Question8(Student[] stud, int cntRecords)
     {
     
     Scanner scan = new Scanner(System.in);
          
          System.out.print("\n Stay on college?(true/false) : ");
          boolean findCollege = scan.nextBoolean();
          
          boolean found = false;
          
          for(int i = 0; i < cntRecords; i++)
          {
               if (stud[i].getStudCollege() .equals ("findCollege"))
               {
                    found = true;
                    System.out.println("\n The record is found");
                    System.out.println(stud[i].toString() );
                    
                    }
               }
               
               if (found == false)
                    System.out.print ("\n The record is not found");
          
     }
     
     
     public static void Question7(Student[] stud, int cntRecords)throws IOException, Exception
     {
          try
          {
               PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter("studentCS220.txt",false)));
               for(int i = 0; i < cntRecords; i++)
          {
               if (stud[i].getStudProg() .equals ("CS220"))
               //if (stud[i].getStudProg() .equalsIgnoreCase ("Cs220"))
                    outfile.print(stud[i].toString());
               }
          
     
               outfile.close();
          }
          
          catch (IOException ice){
               
               System.out.println (ice.getMessage()); //to display type of error
          }
          
          catch (Exception e){
               
               System.out.println (e.getMessage()); //to display type of error
          }
          
          finally{
               
               System.out.println("program end here...");
               }
          
          
     }
     
     public static void Question6(Student[] stud, int cntRecords)
     {
          double sumFee = 0 ;
          for(int i = 0; i < cntRecords; i++)
          {
               if (stud[i].getStudProg() .equals ("CS220"))
               //if (stud[i].getStudProg() .equalsIgnoreCase ("Cs220"))
                    sumFee = sumFee + stud[i].StudentFee();
          }
          System.out.print("\n The total fees that has been collected from CS220 is RM "+sumFee );
          System.out.print("\n");
     }
     
     public static void Question5(Student[] stud, int cntRecords)
     {
          Scanner scan = new Scanner(System.in);
          
          System.out.print("\n Enter a student ID : ");
          String findID = scan.next();
          
          boolean found = false;
          
          for(int i = 0; i < cntRecords; i++)
          {
               if (stud[i].getStudID() .equals (findID))
               {
                    found = true;
                    System.out.println("\n The record is found");
                    System.out.println("\n\n Before update" +stud[i].toString());
                    stud[i].setStudProg("CS225");
                    System.out.println("\n\n After update" +stud[i].toString());
                    break;
                    }
               }
               
               if (found == false)
                    System.out.print ("\n The record is not found");
          
     }

     
     public static void Question4(Student[] stud, int cntRecords)
     {
          int count = 0;
          
          for(int i = 0; i < cntRecords; i++)
          {     
               if (stud[i].getStudProg() .equals ("CS220"))
                    count++;     
          }
          System.out.println("The total is "+count );
     }

     
     public static void Question3(Student[] stud, int cntRecords)
     {
          for(int i = 0; i < cntRecords; i++)
          {
               if (stud[i].getStudProg() .equals ("CS220"))
                    System.out.println(stud[i].toString() );
          }
     }
     
     public static void Question2(Student[] stud, int cntRecords)
     {
          for(int i = 0; i < cntRecords; i++)
          {
               System.out.println(stud[i].getStudID()+ "\t" +stud[i].getStudName());
          }
     }
     
     public static void Question1(Student[] stud, int cntRecords)
     {
          for (int i = 0; i < cntRecords; i++)
          {
               System.out.println(stud[i].toString() ); //display printer
               
          }
     }
}//close class
  • 1. Re: function- boolean
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Welcome to the forum.

    please read this to learn how make better posts in this forum and especially how to format your code examples:
    SQL and PL/SQL FAQ
    -----
    Your Code does not compile for us since it uses some custom classes you did not provide.
    Please turn your example in to a SSCCE ( http://sscce.org ).
    -----
    998481 wrote:
    hi, i've tried to solve my coding but its still have an error at line 153. the question ask me to display the student who got the college. and it use the boolean data type.
    What is the error message you get fom the compiler or the java run time?

    bye
    TPD
  • 2. Re: function- boolean
    1001484 Newbie
    Currently Being Moderated
    package Student;  //nama packages sama dengan file name.
    
    //dalam class tak de main program
    
    public class Student{
         
         //attributes
         
         private String studID; //kene amik 3-4 huruf class
         private String studName;
         private String studProg;
         private String studGender;
         private int studAge;
         private boolean studCollege;
         
         
         //methods/function:
         
         //1.constructor = default constructor, normal constructot, copy constructor
         
         //default constructor= nama method mesti sama dgn class. 
         
         public Student() 
         {
              //initialization
              
              studID = "";
              studName = "";
              studProg = "";
              studGender = "";
              studAge = 0;
              studCollege = true;
              
         }
         
         //normal constructor
         
         public Student(String a, String b, String c, String d, int e, boolean f)
         //parameter refers to attribute. kalau attribute ada 3, parameter pun ada 3.
         {
              //initialization
              
              studID = a;
              studName = b;
              studProg = c;
              studGender = d;
              studAge = e;
              studCollege = f;
              
         }
         
         //dua function nama sama = overloaded function
         
         //2. Setter/ mutator = group and individual setter
         
         //group
         
         public void setStudent(String a, String b, String c, String d, int e, boolean f)
         
         {
              //initialization
              
              studID = a;
              studName = b;
              studProg = c;
              studGender = d;
              studAge = e;
              studCollege = f;
              
         }
         
         //individual setter
         
         public void setStudID (String a)
         {
              studID = a;
         
         }
         
              public void setStudName (String a)
         {
              studName = a;
         
         }
         
              public void setStudProg (String a)
         {
              studProg = a;
         
         }
         
              public void setStudGender (String a)
         {
              studGender = a;
         
         }
         
              public void setStudAge (int a)
         {
              studAge = a;
         
         }
         
              public void setStudCollege (boolean a)
         {
              studCollege = a;
         
         }
         
         //3.getter/accessor/retreive
         
         public String getStudID()
         {
              return studID;
         }
         
         public String getStudName()
         {
              return studName;
         }
         
         public String getStudProg()
         {
              return studProg;
         }
         
         public String getStudGender()
         {
              return studGender;
         }
         
         public int getStudAge()
         {
              return studAge;
         }
         
         public boolean getStudCollege()
         {
              return studCollege;
         }
         
    
         //printer/display
         public String toString()
         {
              return "\n ID     :"+studID+
                      "\n Name : "+studName+
                     "\n Program :" +studProg+
                         "\n Gender : " +studGender+
                       "\n Age : "+studAge+
                      "\n College : "+studCollege;
         }     
         
         //5.Processor
         
         public double StudentFee()
         {
         double fee = 0;
    
         if(studProg.equals ("CS220"))
              fee = 2000;
         else if (studProg.equals ("cs225"))
              fee = 2500;
         else if (studProg.equals ("cs228"))
              fee= 2300;
         else
              fee=0;
         return fee;
         }
    
    
    }   
    the above coding is my class.

    the error shown the boolean cannot be dereferenced.
  • 3. Re: function- boolean
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    In all the <tt>Question*</tt> methods I see the same mistake:
         public static void Question2(Student[] stud, int cntRecords) {
              for (int i = 0; i < cntRecords; i++) {
                   System.out.println(stud.getStudID() + "\t" + stud.getStudName());
              }
         }
    What kind of object is referenced to by the parameter <tt>stud</tt>?
    This would be more visible if you teacher would support propper naming of elements:
         public static void Question2(Student[] studentRecordArray, int cntRecords) {
              for (int i = 0; i < cntRecords; i++) {
                   System.out.println(studentRecordArray.getStudID() + "\t" + studentRecordArray.getStudName());
              }
         }
    Would you bite into a cake box?
    No, You would rather take a cake out of it and bite into that.
    Why don't you do the same with an Array?

    And with a special greeting to your teacher:
    In java Arrays know their length themselves:
         public static void Question2(Student[] studentRecordArray) {
              for (int i = 0; i < studentRecordArray.length; i++) {
    //...
    bye
    TPD

    Edited by: TPD Opitz-Consulting com on 07.04.2013 17:29
  • 4. Re: function- boolean
    1001484 Newbie
    Currently Being Moderated
    but my question is used text file. so we need to use the studentRecordArray also?
  • 5. Re: function- boolean
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    998481 wrote:
    but my question is used text file. so we need to use the studentRecordArray also?
    Currently your code does not compile, so no file will be used at all for now.
    Fix the problem I pointed to.

    bye
    TPD
  • 6. Re: function- boolean
    thomas.behr Newbie
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    Currently your code does not compile, so no file will be used at all for now.
    Fix the problem I pointed to.

    bye
    TPD
    It does compile (at least except for the one problem OP is struggling with). However, without code tags, the forum software thinks the array indexers (using i as index variable) mean "italic". Try quoting OP's original post to see the code with array indexers.

    >
      public static void Question8(Student[] stud, int cntRecords)
      {
      
        Scanner scan = new Scanner(System.in);
        
        System.out.print("\n Stay on college?(true/false) : ");
        boolean findCollege = scan.nextBoolean();
        
        boolean found = false;
        
        for(int i = 0; i < cntRecords; i++)
        {
          if (stud.getStudCollege() .equals ("findCollege")) // <--
    {
    found = true;
    System.out.println("\n The record is found");
    System.out.println(stud[i].toString() );

    }
    }

    if (found == false)
    System.out.print ("\n The record is not found");

    }
    Trying to compare a boolean value with the *String* "findCollege" does not make sense at all. I would think you should rather compare the return value of getStudCollege with your *local variable* findCollege, which happens to be a boolean, too.
    Comparing primitive data is done using the == operator. E.g.
    boolean a = ...;
    boolean b =...;
    if (a == b) {
    ...
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points