2011-09-27 33 views
1
System.out.println("-----------------------------------------------------------"); 
    System.out.println("Please select a task"); 
    System.out.println("1: List employees with details"); 
    System.out.println("2: List of clients in state"); 
    System.out.println("3: List portfolio of client"); 
    System.out.println("4: Release an employee"); 
    System.out.println("5 Display stocks available for purchase/selling"); 
    System.out.println("6: Display client details"); 
    System.out.println("7: Buy Stock for client "); 
    System.out.println("0: Exit program"); 
    System.out.println("-----------------------------------------------------------" + "\n" + "\n"); 

    System.out.print("Input:"); 

    Scanner scan = new Scanner(System.in); 

    input = scan.nextInt(); 

    if (input == 1) 
     { 
     System.out.println("length of array list: " + employeeList.size()); 

     int index = 0; 
     while (index < employeeList.size()) 
      { 
      System.out.println(employeeList.get(index)); 
      index++; 
      } 
     } 
    else if (input == 2) 
     { 
     String state_choice; 
     System.out.println("Please enter the abbrivation for the state:"); 
     state_choice = scan.nextLine(); 

这是我具有[否则,如果(输入== 2)当我尝试运行它不会让我输入的问题我目前的代码的一部分并且实际上忽略它。我认为它是因为前一篇文章中的“\ n”。有没有一种方法可以删除该“\ n”或额外的字符,而不需要在我的输入之前放置另一个scan.nextLine()?没有真正看到任何java文档..字符串用java nextLine()空字符

回答

1

只需在input = scan.nextInt();后加scan.nextLine();。这个问题在input = scan.nextInt();之内,它只读取整数,所以你需要scan.nextLine();放弃按Enter生成的新行\n

1

Eng.Fouad说什么,只scanner.nextLine()input.nextLine()

这是我得到了什么跑就好了:

import java.util.ArrayList; 
import java.util.Scanner; 

public class Testing { 
    private static final ArrayList<String> employeeList = new ArrayList<String>(); 

    public static void main(String[] args) { 

     System.out.println("---------------------------------------------------------"); 
     System.out.println("Please select a task"); 
     System.out.println("1: List employees with details"); 
     System.out.println("2: List of clients in state"); 
     System.out.println("3: List portfolio of client"); 
     System.out.println("4: Release an employee"); 
     System.out.println("5: Display stocks available for purchase/selling"); 
     System.out.println("6: Display client details"); 
     System.out.println("7: Buy Stock for client "); 
     System.out.println("0: Exit program"); 
     System.out.println("---------------------------------------------------------"); 
     System.out.println("\n\n"); 

     System.out.print("Input: "); 

     Scanner scan = new Scanner(System.in); 

     int input = scan.nextInt(); 
     scan.nextLine(); 

     if (input == 1) { 
      System.out.println("length of array list: " + employeeList.size()); 

      int index = 0; 
      while (index < employeeList.size()) { 
       System.out.println(employeeList.get(index)); 
       index++; 
      } 
     } else if (input == 2) { 
      System.out.print("Please enter the abbrivation for the state: "); 
      String state_choice = scan.nextLine(); 
      System.out.println("State: " + state_choice); 

     } 

    } 
} 
+0

是啊,我的错,我只是固定它,谢谢反正:) –