2011-12-09 8 views
-1

すべての助けを借りてくれてありがとうございますが、今度は質問の性質が変更されました。パトリックの提案を使ってループが実行されていますが、入力がそれぞれの配列に格納されていないようです。任意の提案をArrayListに配置しますか?データをArrayListに格納できませんか?

import java.util.ArrayList; 
import java.util.Scanner; 
public class Arrray { 
    public static void main(String [] args){ 

     ArrayList<String> names; 
     ArrayList<String> addr; 


     do { 
      names = new ArrayList<String>(); 
      addr = new ArrayList<String>(); 
      Scanner userInput = new Scanner(System.in); 
      System.out.println("Name and Adreess are: " + names.size() + "**" 
        + addr.size()); 
      System.out.println("please Enter Your Name :"); 
      names.add(userInput.next()); 
      System.out.println("please enter your Address :"); 
      addr.add(userInput.next()); 

      System.out.println("Do you want to add another entry? :(y/n)"); 
      String ans =userInput.next(); // get the value from the user using scanner class 
      if(ans.equals("n") || ans.equals("N")) 
       break; 

     } while (true); 
     int n = names.size(); 
     int a = addr.size(); 
     for(int i =0; i<n && i<a; i++) 
      System.out.println("Name and address are as below: "+ names.get(i)+"**"+ addr.get(i)); 




    } 

} 
+1

あなたは何を試してみましたか? (false)をループ条件として返すことは決してありません。そして、あなたは実際に(y/n)選択のためのユーザー入力を実際に得ようとしますか? – codeling

+1

を投稿する前に少しでも努力してみるのもいいと思うし、各ユーザーのリストを作り直しているので、とにかく動かないだろう。 – viktor

+0

宿題ですか? – Averroes

答えて

0

は、ユーザから値を取得し、ユーザーが入力したNその後、そうでなければ、このような何かをしなければならない、何も

System.out.println("Do you want to add another entry? :(y/n)"); 
String ans = .... // get the value from the user using scanner class 
if(ans.equalsIgnoreCase("n")) 
    break; 
+0

返事をいただきありがとうございますパトリックは助けに感謝します。それは再びループを実行しているが、それはそれが新しいエントリを置き換え続ける配列にデータを格納するように見えない私は取得しようとしている両方のArrayLists任意の提案を埋めることですか? – user980440

0

は、このユーザーの入力

System.out.println("Do you want to add another entry? :(y/n)"); 

をキャプチャし、しばらくその情報を使用してみてください。

1

break statementと一緒にwhile(true)を使用します。

do { 
    if(input.next() == 'n'){ 
     break; 
    } 
} while(true); 
0

壊れていない場合:

String choice = ""; 
do { 
    . 
    . 
    . 
    . 
    System.out.println("Do you want to add another entry? :(y/n)"); 
    choice = userInput.next(); 
} while (!(choice.equals("n") || choice.equals("N"))); 

ライン

choice = userInput.next(); 

は、ユーザ入力、及び入力を比較するStringクラスequals方法を読み取ります。ループは、選択肢がNまたはnになるまで続きます。

0

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

パブリッククラスArray {

public static void main(String[] args) { 

    ArrayList<String> name = new ArrayList<String>(); 
    ArrayList<Integer> phone = new ArrayList<Integer>(); 
    Scanner scanner = new Scanner(System.in); 
    String answer = ""; 
    do { 
     System.out.println("Please enter your name: "); 
     name.add(scanner.next()); 
     System.out.println("Please enter your number: "); 
     phone.add(scanner.nextInt()); 
     System.out.println("Do you want to add a directory y/n?"); 
     answer = scanner.next(); 
    } while (answer.equals("y") || answer.equals("Y")); 
    if (answer.equals("y") || answer.equals("Y")); //want it to go back to start another direcotry here 
    else { 
     System.out.println("Thanks for adding to the directory"); 
     for (int i = 0; i < name.size(); i++) { 
      System.out.print(name.get(i) + "\t"); 
      System.out.print(phone.get(i)); 
      System.out.println(""); 
     } 
    } 
} 

}

関連する問題