Java。使用循环保存控制台输入到数组列表并将其转换为字符串数组

时间:2022-09-02 21:23:58

This is my try to save console input into a array list and convert it to a string Array.

这是我尝试将控制台输入保存到数组列表并将其转换为字符串数组。

There are no syntax errors but it wont work how it should do. For proposal and tips I would be very grateful.

没有语法错误,但它不应该如何工作。对于提案和提示,我将非常感激。

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 个解决方案

#1


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

}

Enjoy!

    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);

#1


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

}

Enjoy!

    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);