【问题标题】:I'm trying to use the scanner with multiple lines and trying to make change the input with another string java我正在尝试使用多行扫描仪并尝试使用另一个字符串 java 更改输入
【发布时间】:2020-08-15 14:05:13
【问题描述】:

所以,我正在尝试制作一个程序,它会询问您想要更改的内容,它会询问您要更改的内容,最好的解释方式是假设我有我的字符串中有一堆 1234,我想将它们全部更改为“WASD”,(请记住,这些多行输入存储在 ArrayList 中)但是,我的程序似乎无法正常工作,它没有给我输入输入后立即输出。

这是我的代码(对不起,我没有遵循命名约定,但我希望你能得到一个大致的理解)

package com.far.main;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class findandreplace {

    static String inputLocation;

    static String inputChange;

    static String data;

    static List<String>test1 = new ArrayList<String>();
    findandreplace(){



    }

    public static void findthanreplace() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter what string YOU WANT TO REPLACE");
        String input = scanner.nextLine();

        inputLocation = input;

        System.out.println("Enter the DATA you want to replace WITH");
        String inputDataChange = scanner.nextLine();

        inputDataChange = inputChange;

        returnInput();
    }

    public static void returnInput() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter in the data");

        while (scanner.hasNextLine()) {
            List<String> test = new ArrayList<>();
            Scanner scanner1 = new Scanner(scanner.nextLine());
            while (scanner1.hasNext()) {
                test.add(scanner1.next());
            }
            test = test1;
        }

        finalResult();
    }


    public static void finalResult() {
        System.out.println();
        System.out.println();

        System.out.println(Collections.replaceAll(test1, inputLocation, inputChange));
        System.out.println(test1);
    }
    public static void main(String[] args) {
        findthanreplace();

    }

}

【问题讨论】:

    标签: java debugging arraylist input replace


    【解决方案1】:

    在我看来,您的代码存在一些问题,例如 1)此代码显示您只需要一行并替换因此您应该编写break; end of while (scanner.hasNextLine()) 范围,因为编译器永远不会退出while。 否则你想要多行你可以得到输入代码的大小,如果大小是空的。 2) 你应该替换 test1 = test; 而不是 test = test1; 3 )您可以将列表项打印为test1.forEach(n-&gt; System.out.print(n.concat(" "))); 而不是System.out.println(test1);

    所以我将您的代码更改如下

    public static void returnInput() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter in the data");
        while (scanner.hasNextLine()) {
            List<String> test = new ArrayList<>();
            Scanner scanner1 = new Scanner(scanner.nextLine());
            while (scanner1.hasNext()) {
                test.add(scanner1.next());
            }
            if(test.isEmpty())
                break;
            test1 .addAll(test);
        }
    
        finalResult();
    }
    
    
    public static void finalResult() {
        System.out.println();
        System.out.println();
    
        System.out.println(Collections.replaceAll(test1, inputLocation, inputChange));
        test1.forEach(n-> System.out.print(n.concat(" ")));
    }
    

    【讨论】:

    【解决方案2】:

    您的代码中有多个问题。应该修复这些以获得预期的结果

    • 您没有提供强制中断接受用户输入的循环的条件。
    • 您使用的Collections.replaceAll() 不符合您的要求。据我了解,您需要用另一个子字符串替换一个子字符串,而不是用另一个元素替换一个元素。因此,在这种情况下,Collections.replaceAll() 不是您想要的。
    • 您正在循环内初始化 Scanner 对象,这不是一个好的做法。
    • 您有多个冗余变量分配。其中一些甚至没有使用。所以把它们清理干净。例如:test = test1;

    这是避免上述问题的一种可能解决方案,但似乎可以进一步优化。

        static String inputLocation;
        static String inputChange;
        static List<String> test1 = new ArrayList<String>();
    
        public static void findthanreplace()
        {
            Scanner scanner = new Scanner( System.in );
            System.out.println( "Enter what string YOU WANT TO REPLACE" );
            inputLocation = scanner.nextLine();
    
            System.out.println( "Enter the DATA you want to replace WITH" );
            inputChange = scanner.nextLine();
    
            returnInput();
        }
    
        public static void returnInput()
        {
            Scanner scanner = new Scanner( System.in );
            System.out.println( "Enter in the data" );
            while( scanner.hasNextLine() )
            {
                String input = scanner.nextLine();
                if( input.equalsIgnoreCase( "DONE" ) ) // Impose a condition to break from this loop, Ex : This loop breaks once user input is DONE
                    break;
                test1.add( input );
            }
    
            finalResult();
        }
    
    
        public static void finalResult()
        {
            System.out.println();
            System.out.println();
    
            for( int i = 0; i < test1.size(); i++ )
            {
    // This line converts the inputs to lowercase and find the starting index of first appearance of the string that needs to be replaced.
                Matcher m = Pattern.compile( inputLocation.toLowerCase() ).matcher( test1.get( i ).toLowerCase() );
                while( m.find() )
                {
                    int startIndex = m.start();
                // Replace the substring with the desired replacement and add that to list
                    String replacedString = test1.get( i ).replace( test1.get( i ).substring( startIndex, inputLocation.length() ), inputChange );                String firstMatch = m.group();
                    test1.set( i, replacedString );
                }
            }
            System.out.println( test1 );
        }
    
        public static void main( String[] args )
        {
            findthanreplace();
        }
    

    【讨论】:

    • 嘿克劳斯,由于某种原因,当我尝试使用多行输入数据时,例如 pastebin.com/mxzMPtwB 并使用 HELLO 作为第一个输入并使用 wasd 作为第二个输入,似乎没有工作
    • @mangoscoconuts 我已经编辑了我的答案,如果这有帮助,请告诉我。我已经对此进行了编辑以删除区分大小写的输入,并且一旦提供了所有字符串输入,您应该添加 DONE 作为输入
    • 非常感谢克劳斯,这对我帮助很大!
    猜你喜欢
    • 1970-01-01
    • 2013-02-20
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多