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 3 of 3

Thread: Array problems...newby...confused?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array problems...newby...confused?

    I need to declare private vars-not static, and need to load arrays then output them to the console. I get some funky output:

    The Baker College Grade Standard is:
    Grade@19821f 90-100
    Grade@addbf1 80-89
    Grade@42e816 70-79
    Grade@9304b1 60-69
    Grade@190d11 50-59
    Grade@a90653 0-49

    My code is:

    Anyone give me some guidance?

    public class Grade {

    private int gradeNumber;
    private char[] letterGrade={'A','B','C','D','E','F'};
    private String[] percent= "90-100","80-89","70-79","60-69","50-59","0-49"};

    public void Grade(){
    letterGrade[0] = ('A');
    letterGrade[1] = ('B');
    letterGrade[2] = ('C');
    letterGrade[3] = ('D');
    letterGrade[4] = ('E');
    letterGrade[5] = ('F');

    }


    public static void main(String args[]) throws IOException {

    Grade[] stgrde = new Grade[6];


    stgrde[0]=new Grade();
    stgrde[1]=new Grade();
    stgrde[2]=new Grade();
    stgrde[3]=new Grade();
    stgrde[4]=new Grade();
    stgrde[5]=new Grade();

    System.out.println("The Baker College Grade Standard is: ");

    for(int i=0;i<6; i++){
    System.out.println(stgrde[i]+" "+ percent[i]);

    }

    }


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array problems...newby...confused?

    here are some modifications that should result in a program that gives the correct output.

    this line:
    private char[] letterGrade={'A','B','C','D','E','F'};
    already initializes and loads up the array with the letters, so the code in the constructor is redundant. you should have an empty constructor.
    what is happening now is that in the line:
    System.out.println(stgrde[i]+" "+ percent[i]);

    stgrade[i] is a Grade object, for which there is no string representation. so what Java does is it prints what i believe is the memory address of the Grade object which is why you get odd output like "Grade@19821f".

    to rectify this, include a method in the Grade class that returns letterGrade and percent array as follows:

    public char[] getLetterGradeArray()
    {
    return letterGrade;
    }

    public String[] getPercentArray()
    {
    return percent;
    }

    now change the System.out.println() statement to the following:
    System.out.println(stgrde[i].getLetterGradeArray()[i]+" "+ stgrde[i].getPercentArray()[i]);

    The creation of the stgrde array in your main method is redundant since you could simply create one grade object and have the output line be as follows:

    Grade grade = new Grade();
    System.out.println(grade.getLetterGradeArray()[i]+" "+ grade.getPercentArray()[i]);

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array problems...newby...confused?

    also IOException is not thrown in the body of main, you can remove the 'throws IOException' clause

Similar Threads

  1. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 11:01 AM
  2. Array problems (printing strings)
    By James5955 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 12:21 AM
  3. Persistence causing problems with JButton 2D Array
    By easyp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2010, 12:21 PM
  4. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 09:48 AM
  5. Newby Question
    By javabob in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2009, 09:09 AM