[英]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.所以,我正在尝试制作一个程序,它会询问您要更改的内容,它会询问您要更改的内容,最好的解释方式是假设我有一堆1234 在我的字符串中,我想将它们全部更改为“WASD”,(请记住,这些多行输入存储到 ArrayList 中)但是,我的程序似乎无法正常工作,它很快就没有给我 output当我放入输入时。
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;
在我看来,您的代码存在一些问题,例如 1)此代码显示您只需要一行并替换,因此您应该编写
break;
end of while (scanner.hasNextLine())
scope because compiler never exit from while. end of
while (scanner.hasNextLine())
scope 因为编译器从不退出 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;
2) 你应该替换
test1 = test;
instead of test = test1;
而不是
test = test1;
3 )you can print items of list as test1.forEach(n-> System.out.print(n.concat(" ")));
3 )您可以将列表项打印为
test1.forEach(n-> System.out.print(n.concat(" ")));
instead of System.out.println(test1);
而不是
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
应该修复这些以获得预期的结果
Collections.replaceAll()
which does not satisfy your requirements.Collections.replaceAll()
不满足您的要求。 As my understanding, you need to replace a substring with another, not an element with another.Collections.replaceAll()
is not what you want in this case.Collections.replaceAll()
不是您想要的。Scanner
object inside a loop and that is not a good practice.Scanner
object,这不是一个好习惯。test = test1;
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.