英文:
I am having issues with the scanner not recognizing certain characters
问题
import java.util.Scanner;
import java.util.TreeSet;
public class setCalculator {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a list of non-negative integers, separated by commas, and enclosed in square brackets. ");
System.out.println("For example: [1, 2, 3] + [4, 3, 10, 0]. ");
while (true) {
System.out.print("\nEnter Sequences: ");
if (input.hasNext("\n")) {
break;
}
try {
compute();
} catch (IllegalArgumentException e) {
System.out.println("Error in input: " + e.getMessage());
}
input.next();
}
}
public static void compute() {
TreeSet<Integer> setA, setB; // The two sets of integers.
setA = readSet();
if (!input.hasNext("\\+") && !input.hasNext("\\-") && !input.hasNext("\\*"))
throw new IllegalArgumentException("Expected *, +, or - after first set.");
setB = readSet();
if (input.hasNext("\n"))
throw new IllegalArgumentException("Extra unexpected input.");
if (input.hasNext("\\+"))
setA.addAll(setB); // Union.
else if (input.hasNext("\\*"))
setA.retainAll(setB); // Intersection.
else
setA.removeAll(setB); // Set difference.
System.out.print("Value: " + setA);
}
private static TreeSet<Integer> readSet() {
TreeSet<Integer> set = new TreeSet<Integer>();
if (!input.hasNext("\\[")) {
throw new IllegalArgumentException("Expected '[' at start of set.");
}
if (input.hasNext("\\[")) {
input.nextLine();
return set;
}
while (true) {
// Read the next integer and add it to the set.
if (!input.hasNextInt())
throw new IllegalArgumentException("Expected an integer.");
int n = input.nextInt(); // Read the integer.
set.add(Integer.valueOf(n)); // (Could have just said set.add(n)!)
if (input.hasNext("\\]")) {
break; // ']' marks the end of the set.
} else if (input.hasNext("\\,")) {
input.next(); // Read a comma and continue.
} else {
throw new IllegalArgumentException("Expected ',' or ']'.");
}
}
input.next(); // Read the ']' that ended the set.
return set;
}
}
英文:
I am new to programming and have never used scanner before. I am having issues with the scanner not recognizing certain characters or at least that is what I think the issue is. The program that I have to make should compute the intersection, union, and set difference of two sets of non-negative integers. The user should input two sets separated by commas, and enclosed in square brackets. For example: [1, 2, 3] + [4, 3, 10, 0]. I was also asked to use TreeSets, and use the appropriate TreeSet method to perform the requested operation on the two sets. At the moment the output that I receive is:
Enter a list of non-negative integers, separated by commas, and enclosed in square brackets.
For example: [1, 2, 3] + [4, 3, 10, 0].
Enter Sequences: [0,1,2,3]+[4,5,6]
Error in input: Expected '[' at start of set.
Any help would be appreciated.
import java.util.Scanner;
import java.util.TreeSet;
public class setCalculator {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a list of non-negative integers, separated by commas, and enclosed in square brackets. ");
System.out.println("For example: [1, 2, 3] + [4, 3, 10, 0]. ");
while(true) {
System.out.print("\nEnter Sequences: ");
if(input.hasNext("\n")) {
break;
} try {
compute();
} catch (IllegalArgumentException e) {
System.out.println("Error in input: " + e.getMessage());
}
input.next();
}
}
public static void compute(){
TreeSet<Integer> setA, setB; // The two sets of integers.
setA = readSet();
if (! input.hasNext("\\+") && ! input.hasNext("\\-") && ! input.hasNext("\\*"))
throw new IllegalArgumentException("Expected *, +, or - after first set.");
setB = readSet();
if( input.hasNext("\n"))
throw new IllegalArgumentException("Extra unexpected input.");
if(input.hasNext("\\+"))
setA.addAll(setB); // Union.
else if (input.hasNext("\\*"))
setA.retainAll(setB); // Intersection.
else
setA.removeAll(setB); // Set difference.
System.out.print("Value: " + setA);
/*
* Start with an empty set.
Read the '[' that begins the set.
Repeat:
Read the next number and add it to the set.
If the next character is ']':
break.
Read the comma that separates one number from the next.
Read the ']'.
Return the set.
*/
}
private static TreeSet<Integer> readSet() {
TreeSet<Integer> set = new TreeSet<Integer>();
if(! input.hasNext("\\[")) {
throw new IllegalArgumentException("Expected '[' at start of set.");
}
if(input.hasNext("\\[")){
input.nextLine();
return set;
}
while (true) {
// Read the next integer and add it to the set.
if (! input.hasNextInt())
throw new IllegalArgumentException("Expected an integer.");
int n = input.nextInt(); // Read the integer.
set.add(Integer.valueOf(n)); // (Could have just said set.add(n)!)
if (input.hasNext("\\)"))
break; // ']' marks the end of the set.
else if (input.hasNext("\\,"))
input.next(); // Read a comma and continue.
else
throw new IllegalArgumentException("Expected ',' or ']'.");
}
input.next(); // Read the ']' that ended the set.
return set;
}
}
答案1
得分: 1
问题可能在于你对 Scanner.hasNext()
的使用。特别是在 if(input.hasNext("\\[")){
这一部分。如果我没有误解,这将检查完整的标记,而不仅仅是 "["。
总体而言,你的方法使问题变得过于复杂。我个人不会通过 Scanner 处理整个任务。更容易的方法是简单地从 Scanner 中获取输入,然后在获取的字符串上进行输入验证。
以下是一个简短的代码片段,它会从 Scanner 中获取输入,将其保存在一个字符串中,并从提供的字符串中解析出 Set
。(但肯定有更有效的解决方案)
编辑:
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the sequence:");
// 读取整个输入
String input = scanner.nextLine();
// 找到运算符
String op = findOperator(input);
// 在 "+" 上拆分字符串
String[] sets = input.split("\\" + op);
if (sets.length != 2) {
// 错误输入,抛出异常
return;
}
TreeSet<Integer> setA = parseSet(sets[0]);
TreeSet<Integer> setB = parseSet(sets[1]);
TreeSet<Integer> resultSet = computeResultSet(setA, setB, op);
System.out.println(resultSet);
}
private static String findOperator(String input) {
char[] operators = { '+', '-', '*' };
for (char c : operators) {
if (input.indexOf(c) != -1) {
return "" + c;
}
}
// 未找到运算符 -> 错误输入 -> 异常处理
return "";
}
private static TreeSet<Integer> parseSet(String input) {
TreeSet<Integer> outputSet = new TreeSet<Integer>();
// 去除空格
input = input.trim();
// 检查输入是否具有正确的格式
if (!input.startsWith("[") || !input.endsWith("]")) {
// 错误输入,抛出异常
return outputSet;
}
// 去除括号
input = input.substring(1, input.length() - 1);
// 将整数添加到集合中
String[] digits = input.split(",");
for (String singleDigit : digits) {
outputSet.add(Integer.parseInt(singleDigit.trim()));
}
return outputSet;
}
private static TreeSet<Integer> computeResultSet(TreeSet<Integer> setA, TreeSet<Integer> setB, String op) {
TreeSet<Integer> resultSet = new TreeSet<Integer>();
if (op.equals("+")) {
setA.addAll(setB);
} else if (op.equals("-")) {
setA.removeAll(setB);
} else if (op.equals("*")) {
setA.retainAll(setB);
}
resultSet = setA;
return resultSet;
}
输入:
Enter the sequence:
[1,2,3] - [2,3]
输出:
[1]
英文:
The issue probably lies in your usage of Scanner.hasNext()
. Especially on if(input.hasNext("\\[")){
. If I'm not mistaken, this will check against the complete token, which is clearly more than only the "[".
Overall, your approach is overcomplicating the issue. I personally would not work through the whole task with the scanner. It's easier to simply take the input from the scanner and then do the input validation on the retrieved String.
This short snippet will take the input from the scanner, save it in a String and parse the Set
s from the provided String. (But there is definitely a more efficient solution possible)
Edit:
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the sequence:");
// read the whole input
String input = scanner.nextLine();
// find the operator
String op = findOperator(input);
// split the String on "+"
String[] sets = input.split("\\" + op);
if (sets.length != 2) {
// raise exception for incorrect input
return;
}
TreeSet<Integer> setA = parseSet(sets[0]);
TreeSet<Integer> setB = parseSet(sets[1]);
TreeSet<Integer> resultSet = computeResultSet(setA, setB, op);
System.out.println(resultSet);
}
private static String findOperator(String input) {
char[] operators = { '+', '-', '*' };
for (char c : operators) {
if (input.indexOf(c) != -1) {
return "" + c;
}
}
// operator not found -> wrong input -> exception handling
return "";
}
private static TreeSet<Integer> parseSet(String input) {
TreeSet<Integer> outputSet = new TreeSet<Integer>();
// remove whitespaces
input = input.trim();
// check if the input has the correct format
if (!input.startsWith("[") || !input.endsWith("]")) {
// exception for incorrect input
return outputSet;
}
// remove the braces
input = input.substring(1, input.length() - 1);
// add the integers to the set
String[] digits = input.split(",");
for (String singleDigit : digits) {
outputSet.add(Integer.parseInt(singleDigit.trim()));
}
return outputSet;
}
private static TreeSet<Integer> computeResultSet(TreeSet<Integer> setA, TreeSet<Integer> setB, String op) {
TreeSet<Integer> resultSet = new TreeSet<Integer>();
if (op.equals("+")) {
setA.addAll(setB);
} else if (op.equals("-")) {
setA.removeAll(setB);
} else if (op.equals("*")) {
setA.retainAll(setB);
}
resultSet = setA;
return resultSet;
}
Input:
Enter the sequence:
[1,2,3] - [2,3]
Output:
[1]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论