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

Thread: Java class Person: with friend_data.txt issue:

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Java class Person: with friend_data.txt issue:

    Your Tester class main method must use an array or an ArrayList of Person objects. It must
    be populated from a text data file, friends_data.txt. Add the following data to your text
    file,
    Michelle, 12/20/2008, Camilla
    Bryan, 3/8/2007, Tom
    Camilla, 6/7/2005, Michelle
    Tom, 10/15/2007, Bryan
    Charlotte, 3/2/2008, Michelle
    Each line has the meaning:
    Person name, Person date of birth (MM/DD/YYYY), name of best friend
    Write a Java program that reads in the data from the friends_data.txt file, allocates a new
    Person for each and then stores the newly created object in either an Array or an ArrayList.
    Next print out a report showing the following information for each Person,
    1. The Person’s name
    2. Their popularity counter
    3. Their age on May 1, 2014
    4. The name of their best friend
    5. The age of their best friend on May 1, 2014
    Finally, print the name of the most popular Person and the name of the oldest Person.

    Person Class

    import java.util.ArrayList;

    public class Person {
    public String personsName;
    public String personsFriend;
    public String personsBirthday;
    public int personsPopularity;
    public int popularity = 0;

    public Person(String aName, String aFriend, String aBirthday) {
    personsName = aName;
    personsFriend = aFriend;
    personsBirthday = aBirthday;
    }

    public String getName() {
    return personsName;
    }

    public String getFriendsName() {
    return personsFriend;
    }

    public String getBirthday() {
    return personsBirthday;
    }

    public int getPopularityNumber(String[] friends) {
    for (int i = 0; i < 5; i++) {
    if (personsName.equals(friends[i])) {
    popularity = 1 + popularity;
    } else
    popularity = popularity;
    }
    return popularity;
    }

    public String getFriend() {
    return personsFriend;
    }

    public int getAge() {
    int MONTH = 0;
    int DAY = 0;
    int YEAR = 0;

    for (int i = 0; i < 3; i++) {
    String[] dates = personsBirthday.split("/");
    String month = dates[0];
    String day = dates[1];
    String year = dates[2];
    MONTH = Integer.parseInt(month);
    DAY = Integer.parseInt(day);
    YEAR = Integer.parseInt(year);
    }

    int age = (2014 - YEAR);
    int moAge = (5 - MONTH);
    if (moAge < 0) {
    age--;
    }
    return age;

    }
    }


    PersonTester

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

    public class PersonTester {
    public static void main(String[] args) throws FileNotFoundException {


    File inputFile = new File("friends_data.txt");
    Scanner in = new Scanner(inputFile);


    final int SIZE = 5;
    Person[] personsOfInterest = new Person[SIZE];
    String[] friends = new String[5];


    int i = 0;
    int popularity = 0;

    String person;

    while (in.hasNextLine()) {
    String line = in.nextLine();
    String[] nameBirthdayname = line.split(", ");

    person = nameBirthdayname[0];
    String birthday = nameBirthdayname[1];
    String friend = nameBirthdayname[2];
    friends[i] = friend;
    personsOfInterest[i] = new Person(person, friend, birthday);

    i++;
    }

    for (i = 0; i < SIZE; i++) {
    System.out.println("");
    System.out.println("New Person");
    System.out.println("--------------------------------------------------------");
    System.out.println("Persons Name : " + personsOfInterest[i].getName());
    System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());
    System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
    System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
    System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
    System.out.println("--------------------------------------------------------");

    }
    }

    }

    I keep getting this error from the compiler:
    System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());

    "method getPopularityNumber in class Person cannot be applied to given type:
    Required: java.lang.String[]; found: no arguments; reason: actual and formal argument lists differ in length

    How do I fix this? What am I doing wrong?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly. Read the error message and THINK about what it's telling you. Come back when you're ready to talk more about it.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Mvxexty1001 (May 6th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation Person Class: with friend data text input compile error.

    Write a Java program that reads in the data from the friends_data.txt file, allocates a new
    Person for each and then stores the newly created object in either an Array or an ArrayList.
    Next print out a report showing the following information for each Person,
    1. The Person’s name
    2. Their popularity counter
    3. Their age on May 1, 2014
    4. The name of their best friend
    5. The age of their best friend on May 1, 2014
    Finally, print the name of the most popular Person and the name of the oldest Person.

    friends_data.text
    Michelle, 12/20/2008, Camilla
    Bryan, 3/8/2007, Tom
    Camilla, 6/7/2005, Michelle
    Tom, 10/15/2007, Bryan
    Charlotte, 3/2/2008, Michelle
    ex.:
    // your code goes here
    import java.util.ArrayList; 
     
    public class Person {
        public String personsName;
        public String personsFriend;
        public String personsBirthday;
        public int personsPopularity;
        public int popularity = 0;
     
        public Person(String aName, String aFriend, String aBirthday) {
            personsName = aName;
            personsFriend = aFriend;
            personsBirthday = aBirthday;
        }
     
        public String getName() {
            return personsName;
        }
     
        public String getFriendsName() {
            return personsFriend;
        }
     
        public String getBirthday() {
            return personsBirthday;
        }
     
        public int getPopularityNumber(String[] friends) {
            for (int i = 0; i < 5; i++) {
                if (personsName.equals(friends[i])) {
                    popularity = 1 + popularity;
                } else
                    popularity = popularity;
            }
            return popularity;
        }
     
        public String getFriend() {
            return personsFriend;
        }
     
        public int getAge() {
            int MONTH = 0;
            int DAY = 0;
            int YEAR = 0;
     
            for (int i = 0; i < 3; i++) {
                String[] dates = personsBirthday.split("/");
                String month = dates[0];
                String day = dates[1];
                String year = dates[2];
                MONTH = Integer.parseInt(month);
                DAY = Integer.parseInt(day);
                YEAR = Integer.parseInt(year);
            }
     
            int age = (2014 - YEAR);
            int moAge = (5 - MONTH);
            if (moAge < 0) {
                age--;
            }
            return age;
     
        }
    }


    ex.:
    // your code goes here
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class PersonTester {
        public static void main(String[] args) throws FileNotFoundException {
     
     
            File inputFile = new File("friends_data.txt");
            Scanner in = new Scanner(inputFile);
     
     
            final int SIZE = 5;
            Person[] personsOfInterest = new Person[SIZE];
            String[] friends = new String[5];
     
     
            int i = 0;
            int popularity = 0;
     
            String person;
     
            while (in.hasNextLine()) {
                String line = in.nextLine();
                String[] nameBirthdayname = line.split(", ");
     
                person = nameBirthdayname[0];
                String birthday = nameBirthdayname[1];
                String friend = nameBirthdayname[2];
                friends[i] = friend;
                personsOfInterest[i] = new Person(person, friend, birthday);
     
                i++;
            }
     
            for (i = 0; i < SIZE; i++) {
                System.out.println("");
                System.out.println("New Person");
                System.out.println("--------------------------------------------------------");
                System.out.println("Persons Name : " + personsOfInterest[i].getName());
                System.out.println("The popular one : " + personsOfInterest[i].getPopularityNumber(friends));
                System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
                System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
                System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
                System.out.println("--------------------------------------------------------");
     
            }
        }
     
    }


    I can not get my tester to compile, I need to find out what is wrong with my code. Please help.

  5. #4
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    Your Tester class main method must use an array or an ArrayList of Person objects. It must
    be populated from a text data file, friends_data.txt. Add the following data to your text
    file,
    Michelle, 12/20/2008, Camilla
    Bryan, 3/8/2007, Tom
    Camilla, 6/7/2005, Michelle
    Tom, 10/15/2007, Bryan
    Charlotte, 3/2/2008, Michelle
    Each line has the meaning:
    Person name, Person date of birth (MM/DD/YYYY), name of best friend
    Write a Java program that reads in the data from the friends_data.txt file, allocates a new
    Person for each and then stores the newly created object in either an Array or an ArrayList.
    Next print out a report showing the following information for each Person,
    1. The Person’s name
    2. Their popularity counter
    3. Their age on May 1, 2014
    4. The name of their best friend
    5. The age of their best friend on May 1, 2014
    Finally, print the name of the most popular Person and the name of the oldest Person.


    Person Class

    import java.util.ArrayList; 
     
    public class Person {
    public String personsName;
    public String personsFriend;
    public String personsBirthday;
    public int personsPopularity;
    public int popularity = 0;
     
    public Person(String aName, String aFriend, String aBirthday) {
    personsName = aName;
    personsFriend = aFriend;
    personsBirthday = aBirthday;
    }
     
    public String getName() {
    return personsName;
    }
     
    public String getFriendsName() {
    return personsFriend;
    }
     
    public String getBirthday() {
    return personsBirthday;
    }
     
    public int getPopularityNumber(String[] friends) {
    for (int i = 0; i < 5; i++) {
    if (personsName.equals(friends[i])) {
    popularity = 1 + popularity;
    } else
    popularity = popularity;
    }
    return popularity;
    }
     
    public String getFriend() {
    return personsFriend;
    }
     
    public int getAge() {
    int MONTH = 0;
    int DAY = 0;
    int YEAR = 0;
     
    for (int i = 0; i < 3; i++) {
    String[] dates = personsBirthday.split("/");
    String month = dates[0];
    String day = dates[1];
    String year = dates[2];
    MONTH = Integer.parseInt(month);
    DAY = Integer.parseInt(day);
    YEAR = Integer.parseInt(year);
    }
     
    int age = (2014 - YEAR);
    int moAge = (5 - MONTH);
    if (moAge < 0) {
    age--;
    }
    return age;
     
    }
    }


    PersonTester


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class PersonTester {
    public static void main(String[] args) throws FileNotFoundException {
     
     
    File inputFile = new File("friends_data.txt");
    Scanner in = new Scanner(inputFile);
     
     
    final int SIZE = 5;
    Person[] personsOfInterest = new Person[SIZE];
    String[] friends = new String[5];
     
     
    int i = 0;
    int popularity = 0;
     
    String person;
     
    while (in.hasNextLine()) {
    String line = in.nextLine();
    String[] nameBirthdayname = line.split(", ");
     
    person = nameBirthdayname[0];
    String birthday = nameBirthdayname[1];
    String friend = nameBirthdayname[2];
    friends[i] = friend;
    personsOfInterest[i] = new Person(person, friend, birthday);
     
    i++;
    }
     
    for (i = 0; i < SIZE; i++) {
    System.out.println("");
    System.out.println("New Person");
    System.out.println("--------------------------------------------------------");
    System.out.println("Persons Name : " + personsOfInterest[i].getName());
    System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());
    System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
    System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
    System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
    System.out.println("--------------------------------------------------------");
     
    }
    }
     
    }



    I keep getting this error from the compiler:
    System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());

    "method getPopularityNumber in class Person cannot be applied to given type:
    Required: java.lang.String[]; found: no arguments; reason: actual and formal argument lists differ in length

    How do I fix this? What am I doing wrong?

    --- Update ---

    I am no expert in java but i think i might know why the error is occurring !

    In your Person class,
    public int getPopularityNumber(String[] friends) {
    for (int i = 0; i < 5; i++) {
    if (personsName.equals(friends[i])) {
    popularity = 1 + popularity;
    } else
    popularity = popularity;
    }
    return popularity;
    }

    You are defining it & you are accepting a parameter 'friends' of String[] type
    public int getPopularityNumber(String[] friends)


    But in your main,
    System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());
    you are not passing a parameter in getPopularityNumber();

    Hence the error
    Required: java.lang.String[]; found: no arguments; reason: actual and formal argument lists differ in length

  6. The Following User Says Thank You to amitection For This Useful Post:

    Mvxexty1001 (May 6th, 2014)

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

    Default Re: Person Class: with friend data text input compile error.

    I can not get my tester to compile,
    Please explain. Copy the full text of the error messages and paste it here.

    Threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Person Class: with friend data text input compile error.

    Error.jpg

    Here is a screen capture of the error.

    The compiler is telling that
    String birthday = nameBirthdayname[1]; in line 36 is
    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:36)
    I am not sure what to do, have stared at this code for over 4 hours now. It’s something I’m just not seeing.
    Last edited by Mvxexty1001; May 6th, 2014 at 09:08 AM. Reason: The screen cap was to small.

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

    Default Re: Java class Person: with friend_data.txt issue:

    Copy the full text of the error messages and paste it here. Text can not be copied from images when making a response.
    Besides the image is too small to read.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    ex.:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class PersonTester {
        public static void main(String[] args) throws FileNotFoundException {
     
     
            File inputFile = new File("friends_data.txt");
            Scanner in = new Scanner(inputFile);
     
     
            final int SIZE = 5;
            Person[] personsOfInterest = new Person[SIZE];
            String[] friends = new String[5];
     
     
            int i = 0;
            int popularity = 0;
     
            String person;
     
            while (in.hasNextLine()) {
                String line = in.nextLine();
                String[] nameBirthdayname = line.split(", ");
     
                person = nameBirthdayname[0];
                String birthday = nameBirthdayname[1];
                String friend = nameBirthdayname[2];
                friends[i] = friend;
                personsOfInterest[i] = new Person(person, friend, birthday);
     
                i++;
            }
     
            for (i = 0; i < SIZE; i++) {
                System.out.println("");
                System.out.println("New Person");
                System.out.println("--------------------------------------------------------");
                System.out.println("Persons Name : " + personsOfInterest[i].getName());
                System.out.println("The popular one : " + personsOfInterest[i].getPopularityNumber(friends));
                System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
                System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
                System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
                System.out.println("--------------------------------------------------------");
     
            }
        }
     
    }


    The blueJ Terminal window is saying this:
    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:36)
    Line 36 is:
    String birthday = nameBirthdayname[1];

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

    Default Re: Java class Person: with friend_data.txt issue:

    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:36)
    That is a runtime error not an error from the compiler.
    It says the code at line 36 used an index into an array that was past the end of the array. The array looks like it has less than 2 elements. An index of 1 would be for the second element.

    The code should make sure the array has the needed elements BEFORE trying to index one. The array field: .length will return the length of the array so it can be tested by the program to be sure it is long enough.

    For debugging, add a println statement to print out the contents of the array:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    ex.:
    // your code goes here
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class PersonTester {
        private static long[] theArrayName;
        public static void main(String[] args) throws FileNotFoundException {
     
     
            File inputFile = new File("friends_data.txt");
            Scanner in = new Scanner(inputFile);
     
     
            final int SIZE = 5;
            Person[] personsOfInterest = new Person[SIZE];
            String[] friends = new String[5];
     
     
            int i = 0;
            int popularity = 0;
     
            String person;
     
            while (in.hasNextLine()) {
                String line = in.nextLine();
                String[] nameBirthdayname = line.split(", ");
     
                person = nameBirthdayname[0];
                String birthday = nameBirthdayname[1];
                String friend = nameBirthdayname[2];
                friends[i] = friend;
                personsOfInterest[i] = new Person(person, friend, birthday);
     
                i++;
            }
     
            for (i = 0; i < SIZE; i++) {
                System.out.println("");
                System.out.println("New Person");
                System.out.println("--------------------------------------------------------");
                System.out.println("Persons Name : " + personsOfInterest[i].getName());
                System.out.println("The popular one : " + personsOfInterest[i].getPopularityNumber(friends));
                System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
                System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
                System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
                System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
                System.out.println("--------------------------------------------------------");
     
            }
        }
     
    }

    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:37)
    This was the result. Not sure if I done this correctly.

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

    Default Re: Java class Person: with friend_data.txt issue:

    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:37)
    Same problem as before. This time on line 37.

    The contents of the println() statement I posted MUST be changed to match the name of the array in the code. It should be placed immediately after the split() statement so you can see what is in the array that the split() statement creates.

    The code is expecting at least 2 elements in the array, but only one is created. Either the data is wrong or the split() statement is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Mvxexty1001 (May 6th, 2014)

  15. #12
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    ex.:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class PersonTester {
        private static long[] theArrayName;
        public static void main(String[] args) throws FileNotFoundException {
     
     
            File inputFile = new File("friends_data.txt");
            Scanner in = new Scanner(inputFile);
     
     
            final int SIZE = 5;
            Person[] personsOfInterest = new Person[SIZE];
            String[] friends = new String[5];
     
     
            int i = 0;
            int popularity = 0;
     
            String person;
     
            while (in.hasNextLine()) {
                String line = in.nextLine();
                String[] nameBirthdayname = line.split(", ");
                System.out.println("an ID "+ java.util.Arrays.toString(nameBirthdayname));
     
                person = nameBirthdayname[0];
                String birthday = nameBirthdayname[1];
                String friend = nameBirthdayname[2];
                friends[i] = friend;
                personsOfInterest[i] = new Person(person, friend, birthday);
     
                i++;
            }
     
            for (i = 0; i < SIZE; i++) {
                System.out.println("");
                System.out.println("New Person");
                System.out.println("--------------------------------------------------------");
                System.out.println("Persons Name : " + personsOfInterest[i].getName());
                System.out.println("The popular one : " + personsOfInterest[i].getPopularityNumber(friends));
                System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
                System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
                System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
                System.out.println("--------------------------------------------------------");
     
            }
        }
     
    }


    This time I got an out put: Which is the best results yet. You rock.

    Terminal Output:
    an ID [Michelle, 12/20/2008, Camilla]
    an ID []
    Error dialog at the bottom as follows:
    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:38)

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

    Default Re: Java class Person: with friend_data.txt issue:

    The String: "an ID" should be changed to identify which array is being printed.

    The output looks like the first results of split() had 3 elements and the second one had 0.
    What is in the variable: line when it is split? Add another println() statement that prints out the contents of the line variable immediately after it is read into. Be sure to add delimiting Strings so all spaces are shown: S.o.p("line="+line+"<");
    If you don't understand my answer, don't ignore it, ask a question.

  17. #14
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    I am new at this, I have no idea what the line variable is: I just write code. Could you be more detailed? Please.

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

    Default Re: Java class Person: with friend_data.txt issue:

    what the line variable is
    I was referring to the variable in the code named: line
    It is defined here:
     String line = in.nextLine();
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Mvxexty1001 (May 6th, 2014)

  20. #16
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    ex.:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class PersonTester {
        private static long[] theArrayName;
        public static void main(String[] args) throws FileNotFoundException {
     
     
            File inputFile = new File("friends_data.txt");
            Scanner in = new Scanner(inputFile);
     
     
            final int SIZE = 5;
            Person[] personsOfInterest = new Person[SIZE];
            String[] friends = new String[5];
     
     
            int i = 0;
            int popularity = 0;
     
            String person;
     
            while (in.hasNextLine()) {
                String line = in.nextLine();
                System.out.println("line="+"line"+"<");
                String[] nameBirthdayname = line.split(", ");
                System.out.println("an ID "+ java.util.Arrays.toString(nameBirthdayname));
     
                person = nameBirthdayname[0];
                String birthday = nameBirthdayname[1];
                String friend = nameBirthdayname[2];
                friends[i] = friend;
                personsOfInterest[i] = new Person(person, friend, birthday);
     
                i++;
            }
     
            for (i = 0; i < SIZE; i++) {
                System.out.println("");
                System.out.println("New Person");
                System.out.println("--------------------------------------------------------");
                System.out.println("Persons Name : " + personsOfInterest[i].getName());
                System.out.println("The popular one : " + personsOfInterest[i].getPopularityNumber(friends));
                System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
                System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
                System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
                System.out.println("--------------------------------------------------------");
     
            }
        }
     
    }

    Not sure if this is what you were after.
    Terminal:
    line=line<
    an ID [Michelle, 12/20/2008, Camilla]
    line=line<
    an ID []
    errors:
    java.lang.ArrayIndexOutOfBoundsException: 1
    at PersonTester.main(PersonTester.java:39)

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

    Default Re: Java class Person: with friend_data.txt issue:

    The println statement should use the variable: line NOT the String "line". Remove the "s so the value of line is printed. See post#14

    The idea of debugging is to see what the computer sees when the code is executed. You need to see what is in the line variable because that determines what the split() method puts into the nameBirthdayname array.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Mvxexty1001 (May 6th, 2014)

  23. #18
    Junior Member
    Join Date
    May 2014
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java class Person: with friend_data.txt issue:

    It compile with no error many thanks.

    --- Update ---

    The program will run until final output:
    System.out.printf("%.1f", " The diameter in feet is : %d%n", diameter);
    error code:
    Java.util.illegalFormatConversionException:
    Null (in java.until.Formatter$FormatSpecifier)
    Terminal error:
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at TankerTruck.Main(TankerTruck.java:26)

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

    Default Re: Java class Person: with friend_data.txt issue:

    Does post#18 belong in this thread? Its contents don't match anything posted here.

    Read the API doc for the printf() method to see how to code it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Is there any intelligent person in the Java programming? plz
    By alaseier in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 4th, 2024, 06:06 AM
  2. Silly issue with java class
    By obadanasr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2013, 02:55 PM
  3. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  4. Replies: 1
    Last Post: September 27th, 2012, 03:55 AM
  5. Issue with setting up different class with arrays
    By D-COD3R in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 06:09 PM