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

Thread: Total noob needing help - Arrays and scanning.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Total noob needing help - Arrays and scanning.

    So I get more or less how arrays work, and I made them work in simple programs with numbers and what not.
    But now I wanna get more complex and say I want to make a small (fleeting) database of people, so I wrote a class for it...

    public class Person {
    	public static int count;
    	private int id;
     
    	// define variables for each object
    	private String name;
    	private String surname;
    	private int age;
    	private int weight;
     
    	public Person() {
    		// set the id equal to the total class count
    		this.id = count;
    		// increase the count
    		count++;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	public void setSurname(String surname) {
    		this.surname = surname;
    	}
     
    	public void setAge(int age) {
    		this.age = age;
    	}
     
    	public void setWeight(int weight) {
    		this.weight = weight;
    	}
     
    	public int getId() {
    		return id;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public String getSurname() {
    		return surname;
    	}
     
    	public int getAge() {
    		return age;
    	}
     
    	public int getWeight() {
    		return weight;
    	}
    }

    I'm assuming the class is fine and that I'm doing something wrong as far as scanning the people into the array...

    Please don't mind the crude programming, I've only been learning for a week.

    import java.util.Scanner;
     
     
    public class Application {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Amount of people?");
    		int n = scan.nextInt();
    		Person[] people = new Person[n];
    		int x = n;
     
    		for (int i=0 ; i < x ; i++ ) {
    			System.out.println("Insert Person number " + (i+1) );
    			String name = scan.nextLine();
    			people[i].setName(name);
    		}
     
    		for (int i=0 ; i < x ; i++ ) {
    			System.out.print(people[i].getName() + " ");
    		}
     
     
     
    	}
    }

    I'm getting

    "Amount of people?
    2
    Insert Person number 1
    Exception in thread "main" java.lang.NullPointerException
    at Application.main(Application.java:34)"

    Of course this line is total garbage:
    people[i].setName(name);

    What am I doing wrong?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Total noob needing help - Arrays and scanning.

    Many n00bs make this mistake. Here's a little story:

    I want to go out and pick some apples. Before that I make a carton to hold the apples. After I have finished making the carton how many apples does it hold?

    Hopefully your answer was zero. Well it's the same for arrays of objects. Once you have declared the array it does not automagically contain any objects.
    Improving the world one idiot at a time!

  3. #3
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Total noob needing help - Arrays and scanning.

    You will also run into the "Scanner Bug". Google it.

    When you try to read in a String after you've taken a numerical input from the Scanner object, you're going to have another 'scratch your head' moment trust me.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Total noob needing help - Arrays and scanning.

    Ok I think I got your suggestions...

    So here's how the new code looks

    Scanner scan = new Scanner(System.in);
    System.out.println("Amount of people?");
    int n = scan.nextInt();
    scan.nextLine(); // scanner bug, flushing out the Enter
    Person[] people = new Person[n];
    int x = n;
     
    for (int i=0 ; i < x ; i++ ) {
    	System.out.println("Insert Person " + (i+1) + " name");
    	String name = scan.nextLine();
    	people[i] = new Person(); //filling the basket with apples
    	people[i].setName(name);
    }
     
    for (int i=0 ; i < x ; i++ ) {
    	System.out.print(people[i].getName() + " ");
    }


    It seems to be working, did I get it right?
    Will a new empty scan.nextLine(); be necessary after each field?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Total noob needing help - Arrays and scanning.

    That seems like it will work. Testing your code will be the best way to find out.

    You only need to call nextLine between reading a single token (eg an int value) and reading a full line or whenever you want to flush the buffer.
    Improving the world one idiot at a time!

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

    MayhemCode (October 9th, 2013)

Similar Threads

  1. total noob needs help
    By rshull1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 05:53 PM
  2. Counting a total and printing value from 2 arrays
    By stewart86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 07:25 AM
  3. Total NOOB question
    By coolidge in forum Java Theory & Questions
    Replies: 3
    Last Post: September 2nd, 2011, 04:09 PM
  4. help w/ storing/scanning numbers in two dimensional arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2010, 11:56 PM
  5. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM

Tags for this Thread