2016-07-20 34 views
-1

问题在于输出。在地址跳转到电子邮件部分之后,我从输入地址的部分(从键盘)获得预期的输出直至联系号码。我已将输出附加在底部。如果有人弄清楚,我会感激我的问题字符串不能输入并跳到下一个提示

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: [email protected] 
+0

你需要清除缓存,即。做一个冲洗。因此,input.nextLine()只是为了重置扫描器,然后再输入地址 – YakuZa

回答

0

添加input.nextLine()这个喜欢后:

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

服用地址输入前简单地使用它。

input= new Scanner(System.in); 

您的代码:

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

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

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

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

,因为它工作正常。但为什么我以前的代码不工作?我以前的代码似乎没有错误。 –

+0

然后请批准我的答案。是的,你没有任何错误,但是,你必须在nextLine()方法之后初始化扫描器对象,以接收另一行字符串 –

+0

当你调用nextLine()时,该行将从缓冲区中移除,并有效地从扫描器中丢弃。如果它有帮助,请批准解决方案。谢谢 –

0

试试这个

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

System.out.print("Number: "); 
contactNumber = Double.parseDouble(input.nextLine()); 

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

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

新线摄于contactAddress在你的代码,你使用nextDouble

-3

nextDouble()是一个类的方法扫描仪,返回下一个扫描的双。

你试过了吗?

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(); 
    } 
0

use input.nextLine(); contactNumber = input.nextDouble();

因为nextDouble方法没有得到回报控制台(\ r),所以ADRESS varialble得到回报char和流动电子邮件输入

相关问题