即使我使用console.nextLine,为什么我的输入中也不能有空格

时间:2018-02-02 02:49:15

标签: java

因此,当我在我的代码上使用.nextLine时,它会在之后填充其余的输入代码。当我摆脱.nextLine的行部分时,它不会用空格来回答。 最终结果会聚集地方输入您的街道地址,请输入您的城市。

import java.util.Scanner;

public class InformationNew
{

   public static void main(String [] args) 
   {   
         //create Scanner class
      Scanner console = new Scanner(System.in);

         //declare varibles
      String firstName, lastName, city, state, upper, lower, streetAdd;
      char firstInt, lastInt;
      int age, size, indexOfSpace;
      double wage;

         //inputs
      System.out.print("Please enter your first name: ");
      firstName = console.next();

      System.out.print("Please enter your last name: ");
      lastName = console.next();

      System.out.print("Please enter your street address: ");
      streetAdd = console.nextLine();

      System.out.print("Please enter your city: ");
      city = console.next();

      System.out.print("Please enter your state: ");
      state = console.next();

      System.out.print("Please enter your age: ");
      age = console.nextInt();

      System.out.print("Please enter your annual income: ");
      wage = console.nextDouble();

      System.out.println();

         //manipulate strings
      indexOfSpace = streetAdd.indexOf( " " );

      firstInt = firstName.charAt(0);

      lastInt = lastName.charAt(0);

      size = city.length();



         //outputs
      System.out.println("User Information: ");
      System.out.println("***************** ");

      System.out.println("Your Name: " + lastName + ", " + firstName);
      System.out.println("Your Initials: " + firstInt + lastInt);
      System.out.println("Characters in city name: " + size);
      System.out.println("User Location (uppercase): " + city.toUpperCase() + ", 
      " + state.toUpperCase());
      System.out.println("User Location (lowercase): " + city.toLowerCase() + ", 
      `enter code here`" + state.toLowerCase());
      System.out.println("Your age: " + age);
      System.out.printf("Your annual income: $%.2f", wage);


   }//end main


}//end class

结果

请输入您的名字:Jon
请输入您的姓氏:Doe
请输入您的街道地址:请输入您的城市:

0 个答案:

没有答案
相关问题