Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: filling an array with strings

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default filling an array with strings

    Hi,

    I am trying to fill an array with strings but the code does not work:

    	static int numStudents = 20;
        Student[] students = new Student[numStudents];
        String theFiller = "0";
     
    	Arrays.fill(students, theFiller);

    I am getting:

    Exception in thread "main" java.lang.ArrayStoreException: java.lang.String
    at java.util.Arrays.fill(Arrays.java:2710)
    at java.util.Arrays.fill(Arrays.java:2685)
    at StudentDatabase.addStudent(StudentDatabase.java:90 )
    at StudentDatabase.main(StudentDatabase.java:51)

    when I try to use for loop:

    		for (int i=0; i<length; i++)
    		{
    			students[i] = "0";
     
    		}
    I am getting "incompatible types"

    I have set/get methods set for class Student, can you help me prefill this array please?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: filling an array with strings

    Can we see the Student class?

    The first part of the code seems to be correct. Make sure you import java.util.Arrays;

    Why are you doing students[i] = "0"; in the for loop?

    Arrays.fill(students, theFiller); is filling the Array with the theFiller value.

    If you want to print out the contents of the Array, try:

    		for (int i = 0; i < students.length; i++) {
    			System.out.println(students[i]);
    		}

    I have moved this thread to - Collections and Generics - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: filling an array with strings

    Student class


    import java.io.*;
    import java.util.*;
     
    public class Student
    {
      private String firstName;
      private String lastName;
      private String enrollmentDate; // better would be to use util.Date not String ??
      private Scanner scan;
     
      // default constructor - initializes default attr values
    	public Student()
       	{
       	}
     
        public Student(String newFirstName, String newLastName, String newEnrollmentDate)
        {
          this.firstName = newFirstName;
          this.lastName = newLastName;
          this.enrollmentDate = newEnrollmentDate;
        }
     
    //setters
    	public void setFirstName (String newFirstName)
    	{
    		firstName = newFirstName;
    	}
     
    	public void setLastName (String newLastName)
    	{
    		lastName = newLastName;
    	}
     
    	public void setEnrollmentDate (String newEnrollmentDate)
    	{
    		enrollmentDate = newEnrollmentDate;
    	}
     
    //getters
        public String getFirstName()
        {
          return firstName;
        }
     
        public String getLastName()
        {
          return lastName;
        }
     
        public String getEnrollmentDate()
        {
          return enrollmentDate;
        }
     
    }

    my code is not working with:

    Arrays.fill(students, theFiller);

    the error what I have is:

    Exception in thread "main" java.lang.ArrayStoreException: java.lang.String
    at java.util.Arrays.fill(Arrays.java:2710)
    at java.util.Arrays.fill(Arrays.java:2685)
    at StudentDatabase.addStudent(StudentDatabase.java:88 )
    at StudentDatabase.main(StudentDatabase.java:48)

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: filling an array with strings

    ArrayStoreException - Is generated if the specified value is not of a runtime type that can be stored in the specified array.

    The specified array needs to be a String. If you updated it to:

    String[] students = new String[numStudents];

    The Array.fill() would work. You need to think about your logic and what you are trying to do..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Array problems (printing strings)
    By James5955 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 01:21 AM
  2. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM
  3. NullPointerException when trying to read Strings into an array of myObject
    By sdivinyi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2010, 09:45 AM
  4. Filling an Array?
    By Bascotie in forum Collections and Generics
    Replies: 5
    Last Post: October 14th, 2009, 06:27 PM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM