无法输入字符串并跳到下一个提示

时间:2016-07-20 06:27:19

标签: java string

问题在于输出。我得到预期的输出直到联系号码,然后地址的输入部分(来自键盘)跳过并跳转到电子邮件部分。我已将输出附加在底部。如果有人弄清楚我的问题,我会很感激

import java.util.Scanner;


public class PhoneLibrary {
    Scanner input = new Scanner(System.in);

    String contactName;
    double contactNumber;
    String contactAddress;
    String contactEmail;
    String description;
    boolean favourite;

    void fillForm() {
        System.out.print("Name: ");
        contactName = input.nextLine();

        System.out.print("Number: ");
        contactNumber = input.nextDouble();

        System.out.print("Address: ");
        contactAddress = input.nextLine();

        System.out.print("E-mail: ");
        contactEmail= input.nextLine();
    }

    public static void main(String[] args) {
        PhoneLibrary contact = new PhoneLibrary();
        contact.fillForm();
    }

}

输出

Name: Vector
Number: 9854425655
Address: E-mail: Vector@gmaail.com

5 个答案:

答案 0 :(得分:0)

在此之后添加input.nextLine()

System.out.print("Number: ");
contactNumber = input.nextDouble();

答案 1 :(得分:0)

在输入地址之前,只需使用它。

    Arrays.sort(helpArray);
    int product = 0;
    if(helpArray[helpArray.length-1]>0) //array is sorted in ascending order
         product = helpArray[helpArray.length-1] * Math.max(helpArray[0]*helpArray[1], helpArray[helpArray.length-2] * helpArray[helpArray.length-3]);
    else
        product = helpArray[helpArray.length-1] * Math.min(helpArray[0]*helpArray[1], helpArray[helpArray.length-2] * helpArray[helpArray.length-3]);

您的代码:

input= new Scanner(System.in);

答案 2 :(得分:0)

试试这个

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
IWebElement el =  wait.Until(ExpectedConditions.ElementExists(by));

您在使用nextDouble

时在代码中的contactAddress中获取了新行

答案 3 :(得分:0)

使用 input.nextLine(); 后 contactNumber = input.nextDouble();

因为nextDouble方法没有从控制台返回(\ r),所以adress varialble获取返回字符并流向电子邮件输入

答案 4 :(得分:-3)

void fillForm() { System.out.print("Name: "); contactName = input.nextLine(); System.out.print("Number: "); try{ contactNumber = Integer.parseInt(input.nextLine()); }catch(ParseException e){ throw new ParseException(); } System.out.print("Address: "); contactAddress = input.nextLine(); System.out.print("E-mail: "); contactEmail= input.nextLine(); } public static void main(String[] args) { PhoneLibrary contact = new PhoneLibrary(); contact.fillForm(); } 是类Scanner的一种方法,它返回下一个扫描的双精度。

你尝试过吗?

input type="float" and type="integer"