problem with bufferedReader

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1458 Views - Last Post: 26 March 2011 - 06:37 PM Rate Topic: -----

#1 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

problem with bufferedReader

Posted 26 March 2011 - 01:32 AM

I am having a problem getting this code to work correctly and I am not sure where I am going wrong. I need the user to select an option and then answer a few more questions. All of this is saved in an arrayList.


package Hw3CoursePkg;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Papa Smurf
 */
public class Hw3CMS {


    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {

 try {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String userSelect =null;


boolean found =true;
do{
    Scanner console = new Scanner(System.in);
    System.out.println();

    System.out.println("Please select from the following options: \n"
            + "\n 1.Add a course "
            + "\n 2.Add a student to a course "
            + "\n 3.View available course "
            + "\n 4.Exit from system ");
     int select=reader.read();

     Hw3Course nc=null;
     int courseNum;
     String courseTitle;
     int courseMaxSize;
     



     int studentUID = 0;
     String studentName = null;
     double studentGPA = 0;
     int courseRostorSize = 0;
     String newStudent= studentUID + studentName +studentGPA;

     ArrayList<Hw3Course>courseArr = new ArrayList<Hw3Course>();
     console.next();

     if(select==1 ){
     System.out.print("Enter Course Number: ");
     courseArr.add(nc);
     courseNum=console.nextInt();

     System.out.print("Enter Course Title: ");
     courseArr.add(nc);
     courseTitle=console.next();
   
     System.out.print("Enter Course Max Size: ");
     courseArr.add(nc);
     courseMaxSize=console.nextInt();
    
     if (select==2) {
         for(int i =0;i<courseArr.size()&&found==false;i++){
             if (courseArr.isEmpty()) {
                 System.out.println("There are no courses in the system ");
             }else if (courseArr.size()<=1){
               System.out.print("Enter course number to register for: ") ;
             console.next();
            
            } if (nc.getCourseNum()==courseNum){
             System.out.print("Enter Student UID: ");
             studentUID=console.nextInt();
             System.out.println(" Enter Student Name: ");
             studentName=console.next();
             System.out.print("Enter Student GPA: ");
             studentGPA=console.nextDouble();
             System.out.println("The course"+ courseNum + "now has" + courseRostorSize + "students");
             found=true;

            }else if (nc.getCourseNum()!=courseNum){
                 System.out.println("Sorry, that course was not found. Try another.");

             }
         }
         }
     if(select==3){
         for (int i=0;i< courseArr.size();i++){
             if (courseArr.isEmpty())
             System.out.println("No course to list. "); 
       if(courseArr.size()<=1){
           System.out.println("Listing courses available in the system: /n"
                   +courseArr.get(i).getCourseNum()+courseArr.get(i).getCourseTitle()+ courseArr.get(i).getCourseMaxSize());
       }
                }
     }
     if(select==4){
         found=false;
         System.out.println("Session exited. Thanks!");

     }
    }

}while(found==true);

   } catch (IOException e) {

        } catch (NumberFormatException e) {
            System.out.println("Input by user was not a number.");

        }
}
}

//This is the code that is holding the package Hw3Course package*//

package Hw3CoursePkg;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Papa Smurf
 */
public class Hw3Course {


    private int cNumber=0;
    private String cTitle ="";
    private int cMaxSize = 0;
    private int id=0;
    private int rostorSize= 0;
    private ArrayList<String>roster=new ArrayList<String>();
    private ArrayList<Hw3Course>courseArr=new ArrayList<Hw3Course>();


public Hw3Course(){
    cNumber = 0;
    cTitle="";
    cMaxSize=0;
    rostorSize=0;
    
}
public Hw3Course(int courseNum, String courseTitle, int courseMaxSize){
    cNumber= courseNum;
    cTitle= courseTitle;
    cMaxSize=courseMaxSize;
}


 public void setcNumber (int newcNumber) {
         cNumber= newcNumber;
       }
 public void setcTitle (int newcMaxSize) {
         cMaxSize= newcMaxSize;
    }
public void setrostorSize (int newrostorSize) {
         rostorSize= newrostorSize;
    }

   
    public int getCourseNum(){
        return cNumber;
    }
    public String getCourseTitle(){
        return cTitle;
    }
    public int getCourseMaxSize(){
        return cMaxSize;
    }
    public int addStudent(String newStudent){
        roster.add(newStudent);
        return rostorSize;
    }
 
         
    
         
}




Any direction on where I am wrong would be greatly appreciated. Thank you.

This post has been edited by <sch2550>: 26 March 2011 - 01:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with bufferedReader

#2 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: problem with bufferedReader

Posted 26 March 2011 - 02:58 AM

Are you getting any errors?
Was This Post Helpful? 0
  • +
  • -

#3 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 10:26 AM

No, it does not give me an error, its just not working right. If the user chooses option 1. it should ask them to enter course number, course title and course max size, instead it just reloads the option menu, the same goes for the other three options. If you put in 2 it just reloads the menu and so on with each number selected. Any idea why I can get the user input to start the code the corresponds to that option choice?
Was This Post Helpful? 0
  • +
  • -

#4 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: problem with bufferedReader

Posted 26 March 2011 - 12:11 PM

If you want to use BufferedReader to load int you must use below code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
	public static void main(String[] args) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int choose;
		try {
			choose = Character.getNumericValue(reader.read());
			System.out.println(choose);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#5 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 12:40 PM

I think I did I did it as you showed for my code, but I am still having problems. Maybe it has something to do with my arraylist or something else, I am completely lost on this. Here is what I have now:

 
package Hw3CoursePkg;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Papa Smurf
 */
public class Hw3CMS {


    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {
int choose;
 try {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String userSelect =null;


boolean found =true;
do{
    Scanner console = new Scanner(System.in);
    System.out.println();

    System.out.println("Please select from the following options: \n"
            + "\n 1.Add a course "
            + "\n 2.Add a student to a course "
            + "\n 3.View available course "
            + "\n 4.Exit from system ");
   choose = Character.getNumericValue(reader.read());
   

     Hw3Course nc=null;
     int courseNum;
     String courseTitle;
     int courseMaxSize;
     
    int studentUID = 0;
     String studentName = null;
     double studentGPA = 0;
     int courseRostorSize = 0;
     String newStudent= studentUID + studentName +studentGPA;

     ArrayList<Hw3Course>courseArr = new ArrayList<Hw3Course>();
     console.next();
// if option 1 is selected, system asks for course number, course title, course max size, and saves all input.*//
     if(choose==1 ){
     System.out.print("Enter Course Number: ");
     courseArr.add(nc);
     courseNum=console.nextInt();

     System.out.print("Enter Course Title: ");
     courseArr.add(nc);
     courseTitle=console.next();
   
     System.out.print("Enter Course Max Size: ");
     courseArr.add(nc);
     courseMaxSize=console.nextInt();
    //option 2 checks courseArr for course that was made in option 1, if no course exists then it should print,"There are no courses in the system ", if a course does exist it should ask for student UID, student name, student GPA, and save all these inputs.*// 
     if (choose==2) {
         for(int i =0;i<courseArr.size()&&found==false;i++){
             if (courseArr.isEmpty()) {
                 System.out.println("There are no courses in the system ");
             }else if (courseArr.size()>=1){
               System.out.print("Enter course number to register for: ") ;
             console.next();
            
            } if (nc.getCourseNum()==courseNum){
             System.out.print("Enter Student UID: ");
             studentUID=console.nextInt();
             System.out.println(" Enter Student Name: ");
             studentName=console.next();
             System.out.print("Enter Student GPA: ");
             studentGPA=console.nextDouble();
             System.out.println("The course"+ courseNum + "now has" + courseRostorSize + "students");
             found=true;

            }else if (nc.getCourseNum()!=courseNum){
                 System.out.println("Sorry, that course was not found. Try another.");

             }
         }
         }
// option 3 checks first to see if a course list was crated in option 1, if so it will print out course Number, course title, and course max size.*//
 
     if(choose==3){
         for (int i=0;i< courseArr.size();i++){
             if (courseArr.isEmpty())
             System.out.println("No course to list. "); 
       if(courseArr.size()<=1){
           System.out.println("Listing courses available in the system: /n"
                   +courseArr.get(i).getCourseNum()+courseArr.get(i).getCourseTitle()+ courseArr.get(i).getCourseMaxSize());
       }
                }
     }
//If option four is selected, system should exit*//
     if(choose==4){
         found=false;
         System.out.println("Session exited. Thanks!");

     }
    }

}while(found==true);

   
            } catch (IOException ex) {
             System.out.println("Input by user was not a number.");

        }
}
}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: problem with bufferedReader

Posted 26 March 2011 - 01:04 PM

You are going to have to be more specific. Telling us you're getting problems isn't descriptive. What specific problems are you encountering?
Was This Post Helpful? 0
  • +
  • -

#7 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 01:45 PM

Sorry about that, this is what is happening: I am using NetBeans and when you click run the user option menu comes up with the four choices (1-4) if the user inputs 1 the system then asks them for a few more things ( course number, course title, max student number) this work just fine. It is when the user selects 2, 3 or, 4 instead of running the code that corresponds to each number, the system just reloads the option menu with the four choices for the user. I am try to figure out where my program is wrong that it won't let the user select options 2-4. I hope this is more clear, I am super new with java let me know if I need to explain this better. Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: problem with bufferedReader

Posted 26 March 2011 - 01:50 PM

It looks like your other if statements are contained within the body of this if statement if(choose==1 ){. So they aren't even evaluated.
Was This Post Helpful? 0
  • +
  • -

#9 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 02:28 PM

I think I now have my if(choose==1){ contained so it does not contain the rest, but I am still having the same problem as stated before. This is what I now have


package Hw3CoursePkg;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Papa Smurf
 */
public class Hw3CMS {


    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {
int choose;
 try {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String userSelect =null;


boolean found =true;
do{
    Scanner console = new Scanner(System.in);
    System.out.println();

    System.out.println("Please select from the following options: \n"
            + "\n 1.Add a course "
            + "\n 2.Add a student to a course "
            + "\n 3.View available course "
            + "\n 4.Exit from system ");
   choose = Character.getNumericValue(reader.read());
   choose=console.nextInt();

     Hw3Course nc=null;
     int courseNum = 0;
     String courseTitle= "";
     int courseMaxSize=0;
     
    int studentUID = 0;
     String studentName = null;
     double studentGPA = 0;
     int courseRostorSize = 0;
     String newStudent= studentUID + studentName +studentGPA;

     ArrayList<Hw3Course>courseArr = new ArrayList<Hw3Course>();
    

     if(choose==1 ){
     System.out.print("Enter Course Number: ");
     courseArr.add(nc);
     courseNum=console.nextInt();

     System.out.print("Enter Course Title: ");
     courseArr.add(nc);
     courseTitle=console.next();
   
     System.out.print("Enter Course Max Size: ");
     courseArr.add(nc);
     courseMaxSize=console.nextInt();
    }
    
     if (choose==2) {
         for(int i =0;i<courseArr.size()&&found==false;i++){
             if (courseArr.isEmpty()) {
                 System.out.println("There are no courses in the system ");
             }else if (courseArr.size()>=1){
               System.out.print("Enter course number to register for: ") ;
             console.next();
            
            } if (nc.getCourseNum()==courseNum){
             System.out.print("Enter Student UID: ");
             studentUID=console.nextInt();
             System.out.println(" Enter Student Name: ");
             studentName=console.next();
             System.out.print("Enter Student GPA: ");
             studentGPA=console.nextDouble();
             System.out.println("The course"+ courseNum + "now has" + courseRostorSize + "students");
             found=true;

            }else if (nc.getCourseNum()!=courseNum){
                 System.out.println("Sorry, that course was not found. Try another.");

             }
         }
         }
     if(choose==3){
         for (int i=0;i< courseArr.size();i++){
             if (courseArr.isEmpty())
             System.out.println("No course to list. "); 
       if(courseArr.size()<=1){
           System.out.println("Listing courses available in the system: /n"
                   +courseArr.get(i).getCourseNum()+courseArr.get(i).getCourseTitle()+ courseArr.get(i).getCourseMaxSize());
       }
                }
     }
     if(choose==4){
         found=false;
         System.out.println("Session exited. Thanks!");

     }
    

}while(found !=false);

   
            } catch (IOException ex) {
             System.out.println("Input by user was not correct.");

        }
}
}


Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: problem with bufferedReader

Posted 26 March 2011 - 02:39 PM

First, properly indent your code. It makes reading it and debugging it a lot easier.

In your first if statement, why do you add hc to the ArrayList three times? Especially when it is null, meaning it does not have an Object associated with it.

In your second and third if statements, all your output statements are in the loops. So if the ArrayList is of size 0, then nothing will be displayed.

If you input 4, does the program exit?
Was This Post Helpful? 0
  • +
  • -

#11 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 03:15 PM

Being really new to java I am not 100% on the proper way to indent, but I gave it my best shot. In option one I need to store the users information in the variables: int courseNum, String courseTitle, int courseMaxSize. these three inputs are sent to a constructor that accepts three inputs and creates a new course object. I am wanting to store these these in a variable called Hw3Course nc. I think this may be where I am going wrong to start with. How do I get user inputs to be stored in this variable?
package Hw3CoursePkg;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Papa Smurf
 */
public class Hw3CMS {


    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {

       int choose;

 try {
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String userSelect =null;


boolean found =true;
do{
    Scanner console = new Scanner(System.in);
    System.out.println();

    System.out.println("Please select from the following options: \n"
            + "\n 1.Add a course "
            + "\n 2.Add a student to a course "
            + "\n 3.View available course "
            + "\n 4.Exit from system ");
   choose = Character.getNumericValue(reader.read());
   choose=console.nextInt();

     Hw3Course nc=null;
     int courseNum = 0;
     String courseTitle= "";
     int courseMaxSize=0;
     
     int studentUID = 0;
     String studentName = null;
     double studentGPA = 0;
     int courseRostorSize = 0;
     String newStudent= studentUID + studentName +studentGPA;

     ArrayList<Hw3Course>courseArr = new ArrayList<Hw3Course>();
    

     if(choose==1 ){
             courseArr.add(nc);
        System.out.print("Enter Course Number: ");
                courseNum=console.nextInt();
        
        System.out.print("Enter Course Title: ");
               courseTitle=console.next();

        System.out.print("Enter Course Max Size: ");
               courseMaxSize=console.nextInt();
    }
    
     if (choose==2) {
         for(int i =0;i<courseArr.size()&&found==false;i++){
             if (courseArr.isEmpty()) {
                 System.out.println("There are no courses in the system ");

             }else if (courseArr.size()>=1){
                 System.out.print("Enter course number to register for: ") ;

                console.next();
            
            } if (nc.getCourseNum()==courseNum){
                System.out.print("Enter Student UID: ");
                studentUID=console.nextInt();

                System.out.println(" Enter Student Name: ");
                studentName=console.next();

                System.out.print("Enter Student GPA: ");
                studentGPA=console.nextDouble();

                System.out.println("The course"+ courseNum + "now has" + courseRostorSize + "students");
                found=true;

            }else if (nc.getCourseNum()!=courseNum){
                 System.out.println("Sorry, that course was not found. Try another.");

             }
         }
         }
     if(choose==3){
       for (int i=0;i< courseArr.size();i++){

          if (courseArr.isEmpty())
             System.out.println("No course to list. ");{
       
           }if (courseArr.size() <= 1) {
           System.out.println("Listing courses available in the system: /n"
           +courseArr.get(i).getCourseNum()+courseArr.get(i).getCourseTitle()+ courseArr.get(i).getCourseMaxSize());
       }
                }
    }
     if(choose==4){
         found=false;
         System.out.println("Session exited. Thanks!");

     }
    

}while(found !=false);

   
            } catch (IOException ex) {
             System.out.println("Input by user was not correct.");

        }
}
}



This code contains the package and the constructor Hw3Course

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Papa Smurf
 */
public class Hw3Course {


    private int cNumber=0;
    private String cTitle ="";
    private int cMaxSize = 0;
    private int id=0;
    private int rostorSize= 0;
    private ArrayList<String>roster=new ArrayList<String>();
    private ArrayList<Hw3Course>courseArr=new ArrayList<Hw3Course>();


public Hw3Course(){
    cNumber = 0;
    cTitle="";
    cMaxSize=0;
    rostorSize=0;
    
}
public Hw3Course(int courseNum, String courseTitle, int courseMaxSize){
    cNumber= courseNum;
    cTitle= courseTitle;
    cMaxSize=courseMaxSize;
}


 public void setcNumber (int newcNumber) {
         cNumber= newcNumber;
       }
 public void setcTitle (int newcMaxSize) {
         cMaxSize= newcMaxSize;
    }
public void setrostorSize (int newrostorSize) {
         rostorSize= newrostorSize;
    }

   
    public int getCourseNum(){
        return cNumber;
    }
    public String getCourseTitle(){
        return cTitle;
    }
    public int getCourseMaxSize(){
        return cMaxSize;
    }
    public int addStudent(String newStudent){
        roster.add(newStudent);
        return rostorSize;
    }
 
         
    
         
}


Was This Post Helpful? 0
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: problem with bufferedReader

Posted 26 March 2011 - 03:18 PM

In NetBeans ALT-SHIFT-F will format your code.
Was This Post Helpful? 1
  • +
  • -

#13 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 03:39 PM

Thanks for showing me how to format.here is what I have:

In option one I need to store the users information in the variables: int courseNum, String courseTitle, int courseMaxSize. these three inputs are sent to a constructor that accepts three inputs and creates a new course object. I am wanting to store these these in a variable called Hw3Course nc. I think this may be where I am going wrong to start with. How do I get user inputs to be stored in this variable?
package Hw3CoursePkg;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Papa Smurf
 */
public class Hw3CMS {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int choose;

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String userSelect = null;


            boolean found = true;
            do {
                Scanner console = new Scanner(System.in);
                System.out.println();

                System.out.println("Please select from the following options: \n"
                        + "\n 1.Add a course "
                        + "\n 2.Add a student to a course "
                        + "\n 3.View available course "
                        + "\n 4.Exit from system ");
                choose = Character.getNumericValue(reader.read());
                choose = console.nextInt();

                Hw3Course nc = null;
                int courseNum = 0;
                String courseTitle = "";
                int courseMaxSize = 0;

                int studentUID = 0;
                String studentName = null;
                double studentGPA = 0;
                int courseRostorSize = 0;
                String newStudent = studentUID + studentName + studentGPA;

                ArrayList<Hw3Course> courseArr = new ArrayList<Hw3Course>();


                if (choose == 1) {
                    courseArr.add(nc);
                    System.out.print("Enter Course Number: ");
                    courseNum = console.nextInt();

                    System.out.print("Enter Course Title: ");
                    courseTitle = console.next();

                    System.out.print("Enter Course Max Size: ");
                    courseMaxSize = console.nextInt();
                }

                if (choose == 2) {
                    for (int i = 0; i < courseArr.size() && found == false; i++) {
                        if (courseArr.isEmpty()) {
                            System.out.println("There are no courses in the system ");

                        } else if (courseArr.size() >= 1) {
                            System.out.print("Enter course number to register for: ");

                            console.next();

                        }
                        if (nc.getCourseNum() == courseNum) {
                            System.out.print("Enter Student UID: ");
                            studentUID = console.nextInt();

                            System.out.println(" Enter Student Name: ");
                            studentName = console.next();

                            System.out.print("Enter Student GPA: ");
                            studentGPA = console.nextDouble();

                            System.out.println("The course" + courseNum + "now has" + courseRostorSize + "students");
                            found = true;

                        } else if (nc.getCourseNum() != courseNum) {
                            System.out.println("Sorry, that course was not found. Try another.");

                        }
                    }
                }
                if (choose == 3) {
                    for (int i = 0; i < courseArr.size(); i++) {

                        if (courseArr.isEmpty()) {
                            System.out.println("No course to list. ");
                        }
                        {
                        }
                        if (courseArr.size() <= 1) {
                            System.out.println("Listing courses available in the system: /n"
                                    + courseArr.get(i).getCourseNum() + courseArr.get(i).getCourseTitle() + courseArr.get(i).getCourseMaxSize());
                        }
                    }
                }
                if (choose == 4) {
                    found = false;
                    System.out.println("Session exited. Thanks!");

                }


            } while (found != false);


        } catch (IOException ex) {
            System.out.println("Input by user was not correct.");

        }
    }
}


This code contains the package and the constructor Hw3Course
package Hw3CoursePkg;

import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Papa Smurf
 */
public class Hw3Course {

    private int cNumber = 0;
    private String cTitle = "";
    private int cMaxSize = 0;
    private int id = 0;
    private int rostorSize = 0;
    private ArrayList<String> roster = new ArrayList<String>();
    private ArrayList<Hw3Course> courseArr = new ArrayList<Hw3Course>();

    public Hw3Course() {
        cNumber = 0;
        cTitle = "";
        cMaxSize = 0;
        rostorSize = 0;

    }

    public Hw3Course(int courseNum, String courseTitle, int courseMaxSize) {
        cNumber = courseNum;
        cTitle = courseTitle;
        cMaxSize = courseMaxSize;
    }

    public void setcNumber(int newcNumber) {
        cNumber = newcNumber;
    }

    public void setcTitle(int newcMaxSize) {
        cMaxSize = newcMaxSize;
    }

    public void setrostorSize(int newrostorSize) {
        rostorSize = newrostorSize;
    }

    public int getCourseNum() {
        return cNumber;
    }

    public String getCourseTitle() {
        return cTitle;
    }

    public int getCourseMaxSize() {
        return cMaxSize;
    }

    public int addStudent(String newStudent) {
        roster.add(newStudent);
        return rostorSize;
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: problem with bufferedReader

Posted 26 March 2011 - 05:22 PM

Why is your Hw3Course class storing a List<Hw3Course> objects? The Hw3Course class should model a single Hw3Course object.
private ArrayList<Hw3Course> courseArr = new ArrayList<Hw3Course>();  



As for instantiating a new Hw3Course object, you can do such like so:
courseNum = console.nextInt();  
courseTitle = console.next();  
courseMaxSize = console.nextInt();  

nc = new Hw3Course(courseNum, courseTitle, courseMaxSize);



Now after you instantiate the new Hw3Course object, add nc to the List only once.
Was This Post Helpful? 0
  • +
  • -

#15 <sch2550>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-January 11

Re: problem with bufferedReader

Posted 26 March 2011 - 06:04 PM

When I insert:
private ArrayList<Hw3Course> courseArr = new ArrayList<Hw3Course>();  




I get an error (illegal start of expression) Why is that?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2