1

I can't seem to figure out why the string college doesn't show when I run this. There are no errors when I compile it. What am I doing wrong? Everything else is working. It's probably an easy fix, but I'm new and just starting to learn Java.

import java.util.Scanner;
public class story_a_holloway{
public static void main(String[] args){
   String name;
   String city;
   int age;
   String college;
   String profession;
   String animal;
   String petname;

   Scanner keyboard = new Scanner(System.in);

   // Get name
   System.out.print("What is your name? ");
   name = keyboard.nextLine();

   // Get city
   System.out.print("What city do you live in? ");
   city = keyboard.nextLine();

   // Get age
   System.out.print("What is your age? ");
   age = keyboard.nextInt();

   // Get college
   System.out.print("What college do you attend? ");
   college = keyboard.nextLine();

   keyboard.nextLine();
   // Get profession
   System.out.print("What is your profession? ");
   profession = keyboard.nextLine();

   // Get animal
   System.out.print("What is your favorite animal? ");
   animal = keyboard.nextLine();

   // Get pet name
   System.out.print("What would you name your pet? ");
   petname = keyboard.nextLine();

   System.out.println("There once was a person named " + name + " who lived in " + city + ". At the age of " + age + ", " + name + " went to college at " + college + ". " + name + " graduated and went to work as a " + profession + ". Then " + name + " adopted a(n) " + animal + " named " + petname + ". They both lived happily ever after!");
}
}
Alyson
  • 13
  • 2

4 Answers4

2

Call keyboard.nextLine() after age = keyboard.nextInt();.

Currently int value is read as age and college = keyboard.nextLine(); reads the remainder of the line which container your int, which is empty. So the correct form should be:

// Get age
System.out.print("What is your age? ");
age = keyboard.nextInt();
keyboard.nextLine();

// Get college
System.out.print("What college do you attend? ");
college = keyboard.nextLine();

Other possible solution to avoid the extra call to nextLine() is reading the whole line as a String and then parsing that String to an integer, for example:

age = Integer.parseInt(keyboard.nextLine());
Bohuslav Burghardt
  • 29,945
  • 7
  • 98
  • 104
  • you could also use two `Scanner` objects: one for string inputs and one for numeric. However, this is overkill. The important issue here is to know that, in order to reuse a `Scanner` object, it must be completely consumed. As it was explained, some `nextXXX()`methods leave the new line character. Using the `nextLine()` method is a workaround to empty the input stream so that the next time you try to get the next string it works the way it's supposed to. – hfontanez Nov 18 '14 at 22:16
0

Put keyboard.nextLine() after this line:

int age=keyboard.nextInt();

This is a common problem that usually happens when you use nextLine() method after nextInt() method of Scanner class.

What actually happens is that when the user enters an integer at int age = keyboard.nextInt();, the scanner will take the digits only and leave the new-line character \n. So you need to do a trick by calling keyboard.nextLine(); just to discard that new-line character and then you can call String college = keyboard.nextLine(); without any problem.

this is taken from Reading Strings next() and nextLine() Java

Community
  • 1
  • 1
MrSimpleMind
  • 6,334
  • 2
  • 33
  • 42
  • 2
    If you know that question is duplicate of another question don't repost its answers but flag it as duplicate. – Pshemo Nov 18 '14 at 22:13
  • Pshemo I actually did flag it as duplicate too, strange that its not shown in the comment field under the question. It did say before that mrsimplemind flag it as duplicate and the url. Thanks anyway for the clarification. – MrSimpleMind Nov 19 '14 at 08:48
0

The problem with your code is using nextLine() after nextInt() & you have an additional nextLine()

// Get age
System.out.print("What is your age? ");
age = keyboard.nextInt();

// Get college
System.out.print("What college do you attend? ");
college = keyboard.nextLine();

keyboard.nextLine(); --> this line
// Get profession
System.out.print("What is your profession? ");
profession = keyboard.nextLine();

What you should do is :

// Get age
System.out.print("What is your age? ");
age = keyboard.nextInt();
keyboard.nextLine(); -->add this line

// Get college
System.out.print("What college do you attend? ");
college = keyboard.nextLine();

// keyboard.nextLine(); --> remove this line
// Get profession
System.out.print("What is your profession? ");
profession = keyboard.nextLine();

A better way of doing it would be : (read about Integer.pasrseInt(String))

// Get age
System.out.print("What is your age? ");
age = Integer.parseInt(keyboard.nextLine());
StackFlowed
  • 6,575
  • 1
  • 26
  • 41
0
    import java.util.Scanner;
    public class story_a_holloway{
    public static void main(String[] args){
        String name;
        String city;
        int age;
        String profession;
        String animal;
        String petname;
        String college;
        Scanner keyboard = new Scanner(System.in);

        // Get name
        System.out.print("What is your name? ");
        name = keyboard.nextLine();

        // Get city
        System.out.print("What city do you live in? ");
        city = keyboard.nextLine();

        // Get age
        System.out.print("What is your age? ");
        age = Integer.parseInt(keyboard.nextLine());

        // Get college
        System.out.print("What college do you attend? ");
        college = keyboard.nextLine();

        // Get profession
        System.out.print("What is your profession? ");
        profession = keyboard.nextLine();

        // Get animal
        System.out.print("What is your favorite animal? ");
        animal = keyboard.nextLine();

        // Get pet name
        System.out.print("What would you name your pet? ");
        petname = keyboard.nextLine();
        System.out.println(college);
        System.out.println("There once was a person named " + name + " who lived in " + city + ".    At the age of " + age + ", " + name + " went to college at " + college + ". " + name + " graduated and went to work as a " + profession + ". Then " + name + " adopted a(n) " + animal + " named " + petname + ". They both lived happily ever after!");
    }
}
Ron
  • 3
  • 2