2011-09-27 3 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(); 

Ceci est une partie de mon code actuel J'ai des problèmes à [else if (input == 2)] quand je tente de l'exécuter ne me laisse pas entrer et juste l'ignore réellement. Je pense que c'est à cause du "\ n" de l'entrée précédente. Est-il possible de supprimer ce caractère "\ n" ou supplémentaire sans mettre un autre scan.nextLine() avant mon entrée? N'a pas vraiment vu quoi que ce soit dans docs java ..Strings java caractère nul avec nextLine()

Répondre

1

Il suffit d'ajouter scan.nextLine(); après input = scan.nextInt();. Le problème est dans input = scan.nextInt();, il lit uniquement le nombre entier, vous devez donc scan.nextLine(); pour supprimer la nouvelle ligne \n générée en appuyant sur Entrée.

1

Qu'est-ce Eng.Fouad dit, ne scanner.nextLine() pas input.nextLine();)

Voici ce que je suis en cours d'exécution très bien:

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

ouais, ma faute je viens de la réparer, merci quand même :) –