简体   繁体   中英

I'm trying to use the scanner with multiple lines and trying to make change the input with another string java

So, I'm trying to make a program that will ask you what you want changed, and it will ask you on what is that you're going to change, The best way to explain this is lets say that I have a bunch of 1234 in my string, and I would like to change them all to "WASD", (Please keep in mind that these multiple line inputs are stored into ArrayList) Yet, my program seems not to be working, it gives me no output back soon as i place in an input.

THIS IS MY CODE (I'm sorry I'm not following naming conventions but i hope you can get a general understanding)

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

    }

}

in my opinion your code as some problem like 1) this code display you want just one line and replace so you should write break; end of while (scanner.hasNextLine()) scope because compiler never exit from while. else you want multiple line you can get size of entered code and if size is empty break. 2) you should replace test1 = test; instead of test = test1; 3 )you can print items of list as test1.forEach(n-> System.out.print(n.concat(" "))); instead of System.out.println(test1);

so i change your code as below

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

There are multiple issues in your code. These should be fixed in order to get the expected results

  • You are not providing a condition that forces to break the loop that takes inputs from user.
  • You use Collections.replaceAll() which does not satisfy your requirements. As my understanding, you need to replace a substring with another, not an element with another. So, Collections.replaceAll() is not what you want in this case.
  • You are initializing a Scanner object inside a loop and that is not a good practice.
  • You have multiple redundant variable assignments. Some of these are not even used. So clean them up. Ex: test = test1;

Here's a one possible solution to avoid above problems, but seems it can be optimized futher.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM