2017-02-22 6 views
-1

これは、コンソール入力を配列リストに保存して文字列配列に変換しようとしたものです。Java。コンソール入力を配列リストのループで保存し、文字列配列に変換する

構文エラーはありませんが、どのように処理する必要がありますか? 提案とヒントについては非常に感謝しています。

import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import java.util.List; 
import java.util.ArrayList; 

public class LoeffelSprache 
{ 
    public LoeffelSprache() 
    { 
    } 
    public void translation() 
    { 
     System.out.println("Insert your Text here"); 
     Scanner sc = new Scanner(System.in); 
     String s= sc.next(); 

     List<String> list1 = new ArrayList<String>(); 

     while (sc.hasNext()) { 
      sc.next(); 
      list1.add(s); 
     } 
     String [] words = new String [list1.size()]; 
     words=list1.toArray(words); 
    } 
} 
+1

あなたの 'main'メソッドはどこですか?あなたは 'translation()'を呼び出していますか? – brso05

+1

'...しかし、それはどうすればいいのかは分かりません。 'どのように機能するのですか?あなたは、あなたの質問にそれを再現するのに必要な、必要な動作、特定の問題またはエラー、および最短のコードを含める必要があります。 [mcve]の作成方法をお読みください。 – azurefrog

+0

コンソール入力を配列に保存したいだけです。私はBlueJを使用しているので、主な方法は必要ありません。 public void translation()はコンストラクタとその空です。 –

答えて

1
public class LoeffelSprache { 
public static void main (String args[]) { 
    System.out.println("Insert your Text here"); 
    Scanner scan = new Scanner(System.in); 
    ArrayList<String> arrList = new ArrayList<String>(); 

    while(scan.hasNextLine()){ 
     arrList.add(scan.nextLine()); 
    } 

    String[] strArr = new String[arrList.size()]; 
    arrList.toArray(strArr); 
    for(String str:strArr){ 
      System.out.println(str); 
    } 
} 

}

お楽しみください!

public static boolean translation() { 
    System.out.println("Insert your Text here"); 
    Scanner sc = new Scanner(System.in); 
    List<String> list1 = new ArrayList<String>(); 
    while (sc.hasNextLine()) { 
     list1.add(sc.nextLine()); 
    } 
    String[] words = new String[list1.size()]; 
    list1.toArray(words); 
    test(words); 
    return true; 
} 

public static void test(String[] words) { 
    for (String a : words) { 
     System.out.println(a); 
+0

funktionが追加するため、コンパイルされないメソッドの型に問題があるようです。ブール型でなければなりません。私はそれを返す真のステートメントを与えるときに問題がまだ存在する –

+0

'public boolean translation() { System.out.println("ここにテキストを挿入する "); スキャナsc =新しいスキャナ(System.in); 文字列s = sc.next(); リスト list1 = new ArrayList (); while(sc.hasNextLine()){ sc.next(); list1.add(sc.hasNextLine()); } String [] words = new String [list1.size()]; list1.toArray(words); がtrueを返します。 } public void test(){ for(String a:words){ System.out.println(a); ' –

+0

public static boolean translation(){ \t \t System.out.println("ここにテキストを挿入 "); \t \tスキャナsc =新しいスキャナ(System.in); \t \tリスト list1 = new ArrayList (); \t \t一方(sc.hasNextLine()){ \t \t \t list1.add(sc.nextLine())。 \t \t \t \t String [] words = new String [list1.size()]; \t \t list1。toArray(単語); \t \t test(words); \t \t return true; \t} \tのパブリック静的ボイド試験(文字列[]語){ \t \t(文字列:ワード){ \t \t \tのSystem.out.println(A)。 –

関連する問題