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

Thread: Need HELP understanding scan.nextInt() contructor!

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need HELP understanding scan.nextInt() contructor!

    Hello

    I'm having trouble with a programming project. I understand the problem but I ran out of ideas to resolve the issue. I'm actually relearning java again and practicing before I re-enroll back into a University. Here is my code.

     
    //*****************************************************************************
    // PP 2.3   Author: Gregory Shavers
    // 
    // This project will take input from a user and display demographic & personal
    // information about this person.
    //
    //***************************************************************************** 
     
     
    import java.util.Scanner;
     
     
    public class JavaApplication7 {
     
     
     
       public static void main(String[] args) {
     
        String Name, College, Petname;
        int Age;
        Scanner scan = new Scanner (System.in);
     
        System.out.print ("What is your name? " );
        Name =scan.nextLine();
     
        System.out.print ("What is your age? " );
        Age = scan.nextInt();
     
        System.out.print ("What is your college? " );
        College = scan.nextLine();
     
        System.out.print( "What is your pet name? " );
        Petname = scan.next();
     
       System.out.println(" \n Hello, my name is " + Name + " and I am " +  Age +
                         " years \n old. I'm enjoying my time at " + College + 
                           ", through  \n I miss my pet " +   Petname  + 
                          " very much!" );

    This is the output of my application.

    ***Start of build***
    run:
    What is your name? Gregory B Shavers Jr
    What is your age? 24
    What is your college? What is your pet name? OB

    Hello, my name is Gregory B Shavers Jr and I am 24 years
    old. I'm enjoying my time at , through
    I miss my pet OB very much!
    BUILD SUCCESSFUL (total time: 13 seconds)

    ***End of Build***

    As you can see for some reason my *College* and "Petname* varible string comes out into one line. What I want it to do is to output separately. The scanner object should take two *separete* Variables store them. And re output my answers in the sentences below. I know the issue is my nextLine constuctor but I don't know what lines of code to use to fix this. I checked google and found some answers. Some of them code they did didn't make sense to me. I already busted through my old text book but I couldn't find and examples or hint on how to resolve this problem. I even checked this site to find some answers but I could not find any recent post. Would the community bless on some knowledge on how to properly use the scan.nextInt constuctor. I would greatly appericated.


    Thanks!

    **Happy Coding***


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need HELP understanding scan.nextInt() contructor!

    Careful with what you call a constructor since scan.nextInt(...) isn't a constructor but rather a method, and this distinction is important.

    Your error is common: nextInt() does not handle the end of line (EOL) token, so it is left dangling. The next time you call nextLine() the EOL token gets swallowed messing your program up. The solution -- handle the EOL token yourself by calling nextLine() on the Scanner immediately after calling nextInt() (or nextDouble() or similar).

    i.e.,
        System.out.print ("What is your age? " );
        Age = scan.nextInt();
        scan.nextLine();  // ******* add this! *******
     
        System.out.print ("What is your college? " );
        College = scan.nextLine();

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

    Rain_Maker (December 6th, 2012)

  4. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need HELP understanding scan.nextInt() contructor!

    Thank you very much! I'm relearning the vocabulary for java again. So its going to take me a sec to catch up again. Thanks again works like a charm!

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need HELP understanding scan.nextInt() contructor!

    You're welcome!

Similar Threads

  1. how to use nextInt() to generate random integers?
    By rph in forum Object Oriented Programming
    Replies: 6
    Last Post: January 3rd, 2013, 01:32 AM
  2. nextInt - can not find InputMismatchException
    By Samaras in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 18th, 2012, 06:47 PM
  3. problem with including contructor in method
    By backdoorliberty in forum Object Oriented Programming
    Replies: 7
    Last Post: June 16th, 2012, 11:57 AM
  4. nextint() Method !
    By M7MD in forum Object Oriented Programming
    Replies: 11
    Last Post: October 27th, 2010, 11:47 AM