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

Thread: A little help need to complete my tutorial question

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

    Default A little help need to complete my tutorial question

    I need to write a program that will prompts user to input 5 persons' name and age one by one and store them into an array of Person class.

    The problem of the code below is that, the program will not prompts the user to key in the name for 2nd - 5th persons after successfully insert name and age for 1st person.

    I found a solution is that instead of using scanner.nextLine(), using a scanner.next() will solve the problem. But instead of reading person name of 1 word, I want to read the name with spaces in between as well.

    Sorry for bad english.
    Any help?

    import java.util.Scanner;
     
    class PersonList {
     
        public static void main (String[] args) {
     
            Person [] list = new Person [5];
     
            Scanner scanner = new Scanner(System.in);
            for (int i=0; i<list.length; i++) {
     
               System.out.print("Enter name: ");
               String name = scanner.nextLine();
               System.out.print("Enter age: ");
               int age = scanner.nextInt();
     
               Person p = new Person(name,age);
               add(list, i, p);
     
            }
            display(list);
        }
        public static void add(Person [] persons, int i, Person p) {
     
            persons[i] = p;
        }
        public static void display(Person [] persons) {
     
            for(int i=0; i<persons.length ; i++)
            {
            	System.out.println(persons[i].getName()+ " " + persons[i].getAge() );
            }
        }
    }

    class Person
    {
    	private String name;
    	private int age;
     
    	public Person(String name,int age)
    	{
    		this.name = name;
    		this.age = age;
    	}
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public int getAge()
    	{
    		return age;
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little help need to complete my tutorial question

    It's because you're getting an Exception, right? What is that Exception?

    Hint- Calling nextLine() reads the rest of the current line, including the "enter" at the end of the line. Calling next() or nextInt() only reads in the next word or int, without reading the "enter" at the end of the line. So what do you think happens the first time you call nextInt()?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2017, 11:11 PM
  2. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 03:17 PM
  3. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM
  4. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM