有点麻烦。
基本上,我得到了输入行
5,4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4
public class Pirate {
int singleCoordinateX;
int singleCoordinateY;
PrintStream out;
String position;
Coordinate coordinates;
CoordinateRow newSplitStringRow;
String splitString;
CoordinateRow adjustedSplitStringRow;
CoordinateRow newAdjustedSplitStringRow;
Pirate() {
out = new PrintStream(System.out);
position = "front";
newSplitStringRow = new CoordinateRow();
adjustedSplitStringRow = new CoordinateRow();
newAdjustedSplitStringRow = new CoordinateRow();
}
void start() {
Scanner newCoordinateInputData = UIAuxiliaryMethods.askUserForInput().getScanner();
String singleLineInputString = newCoordinateInputData.nextLine();
splitLineString(singleLineInputString);
printCoordinateRow();
}
void splitLineString(String singleLineInputString) {
String[] newSingleLineStringArray = singleLineInputString.split("=");
for(int i = 0; i < newSingleLineStringArray.length; i++) {
splitString = newSingleLineStringArray[i];
adjustedSplitStringRow = convertCoordinates(splitString);
placeCoordinateRow(adjustedSplitStringRow);
}
}
CoordinateRow convertCoordinates(String splitString) {
Scanner splitStringArrayScanner = new Scanner(splitString);
splitStringArrayScanner.useDelimiter(",");
while(splitStringArrayScanner.hasNext()) {
singleCoordinateX = splitStringArrayScanner.nextInt();
singleCoordinateY = splitStringArrayScanner.nextInt();
coordinates = new Coordinate(singleCoordinateX, singleCoordinateY);
newSplitStringRow.addSingleCoordinateToBack(coordinates);
}
return newSplitStringRow;
}
CoordinateRow placeCoordinateRow(CoordinateRow adjustedSplitStringRow) {
if(position == "front") {
newAdjustedSplitStringRow.addCoordinateRowFront(adjustedSplitStringRow);
position = "back";
} else if(position == "back") {
newAdjustedSplitStringRow.addCoordinateRowBack(adjustedSplitStringRow);
position = "front";
}
return newAdjustedSplitStringRow;
}
void printCoordinateRow() {
for(int i = 0; i < newAdjustedSplitStringRow.numberOfElements; i++) {
out.printf("%d, %d\n", newAdjustedSplitStringRow.newCoordinateArray[i].coordinateX, newAdjustedSplitStringRow.newCoordinateArray[i].coordinateY);
}
}
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Pirate.Pirate.convertCoordinates(Pirate.java:81)
at Pirate.Pirate.splitLineString(Pirate.java:63)
at Pirate.Pirate.start(Pirate.java:47)
at Pirate.Pirate.main(Pirate.java:257)
splitStringArrayScanner.useDelimiter(",");
splitStringArrayScanner.useDelimiter(",|\\s+");
我应该得到的输出 我不确定发生了什么。
(更新的代码)无论出于什么原因,InputMismatchException的catch块无法正常工作。当代码抛出此错误时,catch块不会捕获它。有人知道为什么会这样吗?
我有这个代码,我想捕捉字母异常,但它一直有以下错误: 这是我的代码:
编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答问题。 (更新的代码)无论出于什么原因,InputMismatchException的catch块无法正常工作。当代码抛出此错误时,catch块不会捕获它。有人知道为什么会这样吗?
我正在为一堂课做家庭作业。你必须计算这个月的工资。每次我尝试运行它时,它总是这样说:我如何修复它?线程“main”java.util.InputMismatchException中的异常 java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(扫描仪.java:1485) java.util.Scanner.ne
当我尝试在错误代码下面运行但得到错误时,我也使用了“givenArray[i]=sc.next();或givenArray[i]=sc.nextLine();”但仍然失败。我尝试了许多不同的方法,但仍然得到相同的错误。似乎扫描仪无法读取数组我需要此代码的帮助,请提前谢谢。 提供的输入:输入第一个数组的长度:4并单击Enter, 输入第二个数组的长度:4并单击Enter, 输入第一个数组:2,3,4