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

Thread: How to print information from an array

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to print information from an array

    When I run the application, the memory address prints to the screen. How do I get it to print the information? This is an example of what it is reading from the file.

    Jason White
    265325222
    31
    Computer Science



    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Test
    {
    public static void main(String[] args)
    {
    String lName = null, fName = null, uid = null, major = null;
    int hours = 0, i = 0, ctr = 0;

    Test[] students = null;
    File input = new File("Enrollment.txt");
    Scanner in;

    students = new Test[30];

    try
    {
    in = new Scanner(input);

    while (in.hasNextLine() && i < students.length)
    {
    fName = in.next();
    lName = in.nextLine();
    uid = in.nextLine();
    hours = in.nextInt();
    in.nextLine();
    major = in.nextLine();

    students[i] = new Test();

    System.out.println(students[ctr] = new Test());

    i++;
    }

    ctr++;
    }

    catch (FileNotFoundException e)
    {
    System.out.println("File not found.");
    }
    }
    }


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: How to print information from an array

    It may not be causing a problem right now but have you tried using hasNext() instead of hasNextLine() as the latter may return true even if there is nothing in the next line.

    As for your actual question, it prints the memory address because you're trying to handle the array of objects as though it were just a single object. Your problem can be fixed by using System.out.println(Arrays.toString(yourArray)); . If the array contains other arrays, then you would use System.out.println(Arrays.deepToString(yourArray)) ;. In both cases, you must first import java.util.Arrays.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to print information from an array

    Another possible problem is the mixing of calls to the methods: next() and nextLine()
    See: Java Scanner not working, difference between next() and nextLine() | Rommel Rico

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to print information from an array

    I changed hasNextLine() to hasNext(). System.out.println(Arrays.toString(yourArray)); and System.out.println(Arrays.deepToString(yourArray)) ; didn't seem to work. They both printed the memory address along with a bunch of "null's".

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to print information from an array

    didn't seem to work.
    They are for debugging to show the complete contents of arrays.
    The Classname@hexaddress is the String returned by a class that does not override the toString() method to return useful data from the class. Add a toString() method that returns a String.
    The nulls are the contents of array slots that have no values assigned.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to print information from an array

    that works, thanks

Similar Threads

  1. Replies: 0
    Last Post: February 1st, 2013, 12:25 PM
  2. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  3. [SOLVED] Getting information from a other class
    By lf2killer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 26th, 2012, 10:19 AM
  4. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  5. I cannot get to print out all my information.
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2011, 01:46 PM

Tags for this Thread