[英]Java: regionMatches to compare two strings input by the user
import java.util.Scanner;
public class CompareStrings {
public static void main(String[] args) {
// prompted user input
Scanner input = new Scanner(System.in);
int firstIndex;
int secondIndex;
System.out.print("Enter First String:"); // prompt user
String stringNumberOne = input.next(); // assigns stringNumberOne to user input
System.out.print("Enter Second String:"); // prompt
String stringNumberTwo = input.next(); // assigns stringNumberTwo to user input
System.out.print("Enter Starting Index for First String:"); // prompt
firstIndex = input.nextInt(); // assigns firstIndex to user input
System.out.print("Enter Starting Index for Second String:"); // prompt
secondIndex = input.nextInt(); // assigns secondIndex to user input
System.out.print("Enter Number of Characters to be Compared:"); // prompt
int numberCompared = input.nextInt(); // assigns numberCompared to user input
boolean results = stringNumberOne.regionMatches(firstIndex,
stringNumberTwo, secondIndex, numberCompared);
if (results)
System.out.println(true);
else
System.out.println(false);
}
}
这是我的代码。 我正在尝试使用 String 方法 regionMatches 来比较用户输入的两个字符串。 程序应该提示用户输入两个字符串,第一个字符串的起始索引,第二个字符串的起始索引,以及要比较的字符数。 然后程序应该打印字符串是否相等(真/假)。 比较时忽略字符的大小写。 我已经编写了上面的代码,如果输入一个像“Hello”这样的单词,我就可以正确运行我的程序。 但是,如果我写了一个句子,例如“你好,这是我的 Java 程序”,我会收到一个错误说明
字符串:线程“main”中的异常 java.util.InputMismatchException
然后代码将无法运行。 它突出显示了我的firstIndex = input.nextInt();
的部分firstIndex = input.nextInt();
代码。 任何帮助将不胜感激! 感谢您花时间阅读这篇文章! :)
String stringNumberOne = input.next();
来自next()
的文档:
从此扫描器中查找并返回下一个完整的令牌。
next()
只抓取下一个完整的标记。 (在这种情况下,只有第一个单词)因此,当前当您输入整个句子时,两个next()
调用将被解析,您将尝试将第三个单词解析为int
,这将引发InputMismatchException
异常。 将此行更改为
String stringNumberOne = input.nextLine();
抓取整条线。
在main
方法中编写所有内容在 Java 中并不是一个好习惯。 这是我的回答:
class Test {
final static Scanner sc = new Scanner(System.in);
private String str1;
private String str2;
private int toffset;
private int ooffset;
private int len;
public Test() {
this("", "");
}
public Test(String str1, String str2) {
super();
this.str1 = str1;
this.str2 = str2;
}
@Override
public String toString() {
return this.str1+" and "+this.str2;
}
public Test[] accept() {
Test[] arr = new Test[1];
System.out.print("Enter First String : ");
this.str1 = sc.nextLine();
System.out.print("Enter Second String : ");
this.str2 = sc.nextLine();
System.out.print("Enter toffset : ");
this.toffset = sc.nextInt();
System.out.print("Enter ooffset : ");
this.ooffset = sc.nextInt();
System.out.print("Enter Length : ");
this.len = sc.nextInt();
arr[0] = new Test(str1, str2);
return arr;
}
public void print(Test[] arr)
{
for(Test t : arr)
System.out.println("Strings you want to compare are : "+t);
}
public void compareString() {
System.out.println("Answer : "+str1.regionMatches(toffset ,str2, ooffset, len));
}
}
public class Program {
public static void main(String args[]){
Test t = new Test();
Test[] arr = t.accept();
t.print(arr);
t.compareString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.