Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default char array output Null...Help please

    I am trying to output a char array to the screen and it is printing Null (meaning empty). I am new to arrays and was wondering if my code was right as seen here:

    import java.io.*;
    import java.awt.*;
    import java.lang.*;
    import java.io.IOException;


    public class Grade {

    private int gradeNumber;
    private char[] letterGrade={'A','B','C','D','E','F'};


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

    Grade[] stgrde = new Grade[6];
    for(int i=0;i<6; i++){
    System.out.println(stgrde[i]);
    }

    }


    am I getting the array into the main method the right way?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you declare an array the new keyword creates an array object with a length of x, where x is the number inside []. But it does not fill the array, you still have to add grade objects to it.

  3. #3
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    When you declare an array the new keyword creates an array object with a length of x, where x is the number inside []. But it does not fill the array, you still have to add grade objects to it.
    would i load it inside a constructor?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I think your problem is that in main you created an array of Grade objects, which each have there own array of characters and another instance variable. If this was your intent, then you need to initialize the elements of the array and override a toString.

    I'm assuming you simply meant to create an array of chars and then print them, if this was your goal you want to either declare the grade array inside the main method, or initialize a single instance of the Grade class and then access the instance variable which contains the grades.

    If this isn't clear let me know and I will try to explain it better.

    If I wanted to declare, initialize, and print an array of ints I would do something like this
    Java Code:
    public class X{
      public static void main(String[] args){
        int[] y = {1, 2, 3, 4, 5, 6};
        for(int j = 0; j < y.length; ++j){
          System.out.println(y[j]);
        }
      }
    }
    Also, when posting code wrap it in code tags like this
    [code]
    YOUR CODE HERE
    [/code]

  5. #5
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Yes, that makes sense, but I have to use private declarations outside the static void main. So i have to get the non static array in. I know how to do this with a variable but not with arrays.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toppcon View Post
    Yes, that makes sense, but I have to use private declarations outside the static void main. So i have to get the non static array in. I know how to do this with a variable but not with arrays.
    private and static are orthogonal concepts -- one has nothing to do with the other. So even though you have to use a private variable, it still can be static. If you cannot use a static variable, then you will need to deal with it in methods and constructors of the class.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can the variable be static? If it can it simplifies the process but if not it's still fairly simple.

    If it can be static just declare it as "private static char grades[] = {...}" and you will be able to access it from main. If it can't be static declare the instance variable like this
    Java Code:
    private char grades[];
    Then use a no arg constructor to initialize the array with the values. You can also have a constructor that takes an array and sets the instance variable to the array. If you provide a getter you can access the class variables in main. Here is a non relay example of using a getter. (you can treat the rerun type as an instance of the variable, or set it to something)

    Java Code:
    public class Y{
      private int z;
      private String x;
      public Y(){
        z = 10; 
        x = "hello";
      }
      public String getX(){
        return x;
      }
      public int getZ(){
        return z;
      }
      public static void main(String[] args){
        Y y = new Y();
        System.out.println(y.getZ());
        System.out.println(y.getX());
      }
    }

  8. #8
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    I tried the no arg constructor method...still null. And like I said, I am "fuzzy" on arrays in JAVA-especially when I need to get into a static main. I could get the getter method to compile but no output. Can you give me a little more code?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toppcon View Post
    I tried the no arg constructor method...still null.

    Come on now -- without code, we have absolutely no idea what your problem was in this attempt.


    And like I said, I am "fuzzy" on arrays in JAVA-especially when I need to get into a static main. I could get the getter method to compile but no output. Can you give me a little more code?
    Please let's see your last attempt.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  11. #11
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Java Code:
    public class Grade {
    
    		private int gradeNumber;
    		private char[] letterGrade={'A','B','C','D','E','F'};
    
    public Grade(){
    	char[] letterGrade = {'A','B','C','D','E','F'};
    	
    	}
    
    public static void main(String[] args) throws IOException {
    
    			Grade[] stgrde = new Grade[6];
    			for(int i=0;i<6; i++){
    						System.out.println(stgrde[i]);
    			}
    
    		}
    Still prints null.
    Last edited by Fubarable; 05-08-2011 at 07:00 AM. Reason: code tags added

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toppcon View Post
    Java Code:
    public class Grade {
    
    		private int gradeNumber;
    		private char[] letterGrade={'A','B','C','D','E','F'};
    
    public Grade(){
    	char[] letterGrade = {'A','B','C','D','E','F'};
    	
    	}
    
    public static void main(String[] args) throws IOException {
    
    			Grade[] stgrde = new Grade[6];
    			for(int i=0;i<6; i++){
    						System.out.println(stgrde[i]);
    			}
    
    		}
    Still prints null.
    That's because as was mentioned before, you're creating an array of Grade objects, but not filling the array. You will need two for loops after creating your stgrde array, the first to fill the array with Grade objects, and the second to print them out (you already have the second one created).

    The first for loop would look something like:
    Java Code:
    for (int i = 0; i < stgrde.length; i++) {
       stgrde[i] = //.... here you must assign it a new Grade(...) object.
    }

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The reason it prints null is because you are doing a few things wrong.

    First, the variable in the constructor is local to the constructor. It declares and initializes a variable, but does nothing with it. Iu want to simply initializethe instance variable, but not declare it.

    Another error is that you are creating an array of grade objects. Your class holds an array itself, you want to simply create one instance of the class, from there you can access the instance variable.

  14. #14
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    This is what I have now. I'm creating the new Grade() inside static main and-I think- I am loading it with the letterGrade array. But still nothing.

    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]);

    }

    }

  15. #15
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    This is my output:

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

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    That implies that in your Grade class, you didn't override the toString() method inherited from Object to return something meaningful.

    db

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, since each grade object contains it's own list of grades, you only need one instance of it, not 6.

    If ou override toString you can easily print objects.

    Java Code:
    public class X{
      int x;
      String s;
      public String toString(){
        return s + ": " + x;
      }
    }
    Since a toString method is overridden this class can be printed easily
    Java Code:
    X x = new X();
    System.out.println(x);

  18. #18
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Thanks for your help, I just used the constructor Grade() and called the new Grade from main()...easier. Thanks again.

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Mind showing your code?

  20. #20
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    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 Grade(){
    System.out.println("The Baker College Grade Standard is: ");

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

    }

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

    new Grade();

    }

Page 1 of 2 12 LastLast

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. Replies: 2
    Last Post: 03-08-2011, 07:20 AM
  3. Replies: 6
    Last Post: 10-18-2010, 11:40 AM
  4. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  5. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 09:48 AM

Posting Permissions

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