[英]Check whether the brackets and delimiters contained in a string are balanced
這是簡單的代碼,用於檢查字符串中的方括號/定界符是否平衡-誰能幫忙為什么不起作用! 我確信有很多方法可以提高效率,但是出於當前教程的目的,我想知道為什么它不能以這種形式工作以及當前存在什么問題。
首先,我不能將變量c添加到LinkedList中,我必須使用文字值-在另一個教程中我具有相同的值,並且它可以很好地添加變量。
其次,在某些情況下,它根本不會按照if語句將分隔符添加到我的鏈表中。 字符串“(我想知道(如果)此分隔符)[句子]的評估結果為[balanced}”被評估為“ balanced”,但是從我的代碼中不應該這樣做-請幫助我在這里拉頭發。
最終,我遇到了相同的錯誤,但不是每個字符串都偶爾出現-我隨機輸入某些字符,例如'csadlkfsd kljf [] {} [] [] {'
這返回了錯誤
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at set07102.Delimiter.main(Delimiter.java:16)
第16行是'char c = s.charAt(0);' 據我所知,這不應該發生。
System.out.println(strStack); 只有最后檢查LinkedList -如果它在代碼中如此之遙!
任何幫助都會令人敬畏,謝謝大家。
import java.util.LinkedList;
import java.util.Scanner;
public class Delimiter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a string containing different types of brackets: ");
String str = scanner.nextLine();
String[] strSplit = str.split(" ");
LinkedList<Character> strStack = new LinkedList<>();
System.out.println(" ");
for(String s : strSplit) {
char c = s.charAt(0);
if(c == '('){
strStack.push('(');
}
if( c == '{'){
strStack.push('{');
}
if(c == '['){
strStack.push('[');
}
if(c == '<'){
strStack.push('<');
}
if(c == ')' && strStack.get(0) != '('){
System.out.println("The delimiters in the string " + "'" + str + "'" + " are not balanced!");
break;
}
if(c == ']' && strStack.get(0) != '['){
System.out.println("The delimiters in the string " + "'" + str + "'" + " are not balanced!");
break;
}
if(c == '}' && strStack.get(0) != '{'){
System.out.println("The delimiters in the string " + "'" + str + "'" + " are not balanced!");
break;
}
if(c == '>' && strStack.get(0) != '<'){
System.out.println("The delimiters in the string " + "'" + str + "'" + " are not balanced!");
break;
}
}
System.out.println("The delimiters in the string " + "'" + str + "'" + " are balanced. ");
System.out.println(" ");
System.out.println(strStack);
}
}
這是一種方法。 我不確定它是否完全沒有錯誤,是否可以處理所有情況,但我認為可能已經很接近了。
正如其他用戶所評論的那樣,分割輸入字符串是錯誤的方法。 您將需要遍歷每個字符,並使用堆棧來跟蹤看到的括號以及接下來應該關閉的括號。
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;
public class BalancedBrackets
{
public static void main(String[] args)
{
HashMap<Character,Character> bracketPairs = new HashMap<Character,Character>();
bracketPairs.put('[', ']');
bracketPairs.put('(', ')');
bracketPairs.put('{', '}');
bracketPairs.put('<', '>');
Stack stack = new Stack();
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a string containing different types of brackets: ");
String str = scanner.nextLine();
for(int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if(bracketPairs.keySet().contains(c))
{
stack.push(c);
}
if(bracketPairs.values().contains(c))
{
if(stack.size() == 0)
{
System.out.println("Unexpected closing bracket.");
return;
}
char lastOpen = (char) stack.peek();
char expectedClose = bracketPairs.get(lastOpen);
if(expectedClose == c)
{
stack.pop();
}
else
{
System.out.println("Unexpected closing bracket.");
return;
}
}
}
if(stack.size()==0)
{
System.out.println("String is balanced.");
}
else
{
System.out.println("String is unbalanced.");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.