运行时不显示Java字符串

时间:2014-11-18 21:55:37

标签: java string

我似乎无法弄清楚为什么字符串学院在我运行时没有显示出来。编译时没有错误。我究竟做错了什么?其他一切都在发挥作用。它可能是一个简单的解决方案,但我是新手,刚刚开始学习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!");
}
}

4 个答案:

答案 0 :(得分:2)

keyboard.nextLine()之后致电age = keyboard.nextInt();

当前int值被视为年龄,college = keyboard.nextLine();读取您int的容器的剩余行,该行是空的。所以正确的形式应该是:

// 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();

避免额外调用nextLine()的其他可能解决方案是将整行读取为String,然后将该String解析为整数,例如:

age = Integer.parseInt(keyboard.nextLine());

答案 1 :(得分:0)

keyboard.nextLine()放在此行之后:

int age=keyboard.nextInt();

这是在nextLine()类的nextInt()方法之后使用Scanner方法时通常会发生的常见问题。

实际发生的情况是,当用户在int age = keyboard.nextInt();输入整数时,扫描仪将仅采用数字并保留换行符\n。因此,您需要通过调用keyboard.nextLine();来放弃该换行符,然后您可以毫无问题地调用String college = keyboard.nextLine();

这取自Reading Strings next() and nextLine() Java

答案 2 :(得分:0)

你的代码的问题是在nextInt()&之后使用nextLine()。你有一个额外的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();

你应该做的是:

// 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();

更好的方法是:(阅读Integer.pasrseInt(String)

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

答案 3 :(得分: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!");
    }
}