【发布时间】: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