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

Thread: Homework problem (scanner problems)

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Homework problem (scanner problems)

    I'm supposed to create a program that Asks for 3 names, as well as their age. It should ask one name, then their age, then the next and so on. When I compile this program, here is what happens.

    Enter the persons name:
    Tom
    Enter the persons age
    20
    Enter the persons name:
    Enter the persons age
    Tom
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Oldest.main(Oldest.java:17)
    Enter name shows up, I enter the name, enter age shows up, I enter the age, then the next set comes up together (Enter name, Enter age, without giving me a chance to enter a name before enter age pops up). The rest is the error message

    import java.util.*;
    public class Oldest {
    public static void main(String[] args) {
    	Scanner stdin = new Scanner(System.in);
    	String per1name, per2name, per3name;
    	int per1age, per2age, per3age;
     
    	System.out.println("Enter the persons name: ");
    	per1name = stdin.nextLine();
    	System.out.println("Enter the persons age");
    	per1age = stdin.nextInt();
    	System.out.println("Enter the persons name: ");
    	per2name = stdin.nextLine();
    	System.out.println("Enter the persons age");
    	per2age = stdin.nextInt();
    	System.out.println("Enter the persons name: ");
    	per3name = stdin.nextLine();
    	System.out.println("Enter the persons age");
    	per3age = stdin.nextInt();
     
    	if((per1age > per2age) && (per1age > per3age)){
    		System.out.println(per1name + " is the oldest person with the age " + per1age);
    	}else if((per2age > per1age) && (per2age > per3age)){
    		System.out.println(per2name + " is the oldest person with the age " + per2age);
    	}else
    		System.out.println(per3name + " is the oldest person with the age " + per3age);
    }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Homework problem (scanner problems)

    It's because nextInt() doesn't consume the newline character in the input stream. Try this:
    import java.util.*;
    public class Oldest {
    public static void main(String[] args) {
    	Scanner stdin = new Scanner(System.in);
    	String per1name, per2name, per3name;
    	int per1age, per2age, per3age;
     
    	System.out.println("Enter the persons name: ");
    	per1name = stdin.nextLine();
    	System.out.println("Enter the persons age");
    	per1age = stdin.nextInt();
            stdin.nextLine();
    	System.out.println("Enter the persons name: ");
    	per2name = stdin.nextLine();
    	System.out.println("Enter the persons age");
    	per2age = stdin.nextInt();
            stdin.nextLine();
    	System.out.println("Enter the persons name: ");
    	per3name = stdin.nextLine();
    	System.out.println("Enter the persons age");
    	per3age = stdin.nextInt();
            stdin.nextLine();
     
    	if((per1age > per2age) && (per1age > per3age)){
    		System.out.println(per1name + " is the oldest person with the age " + per1age);
    	}else if((per2age > per1age) && (per2age > per3age)){
    		System.out.println(per2name + " is the oldest person with the age " + per2age);
    	}else
    		System.out.println(per3name + " is the oldest person with the age " + per3age);
    }
    }

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

    TommyFiz (September 20th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Homework problem (scanner problems)

    Awesome that worked. Thanks for the fast reply!

Similar Threads

  1. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM
  2. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  3. Mini Sudoku game 4x4 ( four 2x2 grids) program
    By derky in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2009, 07:39 AM
  4. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM