Results 1 to 3 of 3

Thread: New To Java

  1. #1
    smiley4080 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default New To Java

    Hello EveryBody ;
    if any one can check if my code is correct or not>>> please help me


    class Student that had a list of students in a university.
    The class have private instance variables which are
    ArrayList<String> StudentName – the student names
    int numOfStudent – the number of students in StudentName.

    The class have a one-argument constructor that initializes the instance variables according to a given number of students.


    The class should have a public method void getStudentName () that will ask the user to enter the names of all students one name at a time and store them in Arraylist by Using Scanner class.

    Also, class have a public method void writeNamesToScreen()that write or show the names stored in the list on the screen.

    Also, Class have a public method void writeNamesToFile(string file)that allow us to write names stored in the list to the file “D:\file”,by using PrintWriter class.

    Also class have a main method that when invoked will first create an object name of the class Student with 4students, and then call the other methods .



    Java Code:
    public class Student {
    
    private ArrayList<String>StudentName ;
    private int numOfStudents;
    
    public static void main(String arga[])throws FileNotFoundException
        {
       Student names=new Student(4);
       sn.getStudentName ();
       sn.writeNamesToScreen();
       sn.writeNamesToFile("D:\\BTI.txt");
       }
    
    // constructure
    public Student(int n)
        {	
        numberOfStudents=n;
    StudentName=new ArrayList<String>();
    }
    public void getStudentNames()
        {
           Scanner sc= new Scanner(System.in);
           System.out.print("please enter the names of all the students");
           StudentName= sc.next();
           sc.close();
        }  
    
    public void writeNamesToScreen()
        {
            System.out.println(StudentName);
        }  
    
    public void writeToFile(String file)
        {
            PrintWriter pw= new PrintWriter(file); 
            pw.println(StudentName);
        }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Don't forget ArrayList is a List,
    so StudentName (a List of Strings) = sc.next() (a single String) doesn't make sense.

    Change this:
    StudentName= sc.next();

    To this:
    StudentName.add(sc.next());

    And encapsulate that in a loop where you should loop n times, 'n' being the number of students input.
    There are several ways to do this, one way is to take the whole line like this:
    String input = sc.nextLine();
    Then split the line on each whitespace to get an array of Strings
    String[] inputArray = input.split(" ");
    And, of course, you can add an entire Array to a List like this:
    List<String> myArrayList = new ArrayList<String>(Arrays.asList(inputArray));
    Replacing the previous contents of myArrayList,
    Or like this (appending to the current List):
    myArrayList.addAll(Arrays.asList(inputArray));

    Also, not really a problem, more of good practice

    1. You could've set the initial capacity
    numberOfStudents=n;
    StudentName=new ArrayList<String>(n);
    (since you know the number of students)

    2. You could've used a String[] Array rather than a List
    (again, since you know the number of students)

    3. You could derive the numberOfStudents with StudentNames.size() for ArrayList or StudentNames.length if it were an Array, so there is no need to store it in a separate field.
    Last edited by ozzyman; 04-28-2011 at 12:41 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •