這是我正在使用的代碼。它決議一個文本檔案并將結果列印到螢屏上。它決議的文本具有它遵循的特定格式,因此大多數方法都是重復的。輸入文本如下:
我需要把它變成一個遞回的體面決議器。我完全誤解了我應該做什么。 程式讀取的輸入檔案
public class FileParser {
private static Scanner scan;
private static String getLabel(String labelFile) {
int i;
labelFile.trim();
for (i = 0; i < labelFile.length(); i ) {
char charVal = labelFile.charAt(i);
boolean bool = Character.isLetter(charVal);
if (!bool) {
break;
}
}
return labelFile.substring(0, i);
}
public static void main(String[] args) {
try {
File inputFile = new File("C:/Users/corey/OneDrive/Desktop/Input.txt");
String mainStr, mainLabel;
scan = new Scanner(inputFile);
if (scan.hasNextLine()) {
mainStr = scan.nextLine().trim();
mainLabel = getLabel(mainStr);
if (!mainLabel.equalsIgnoreCase("Window")) {// gui ::= Window
System.out.println("Incorrect Input file should start with WINDOW");
return;
}
mainStr = mainStr.substring(mainLabel.length()).trim();
JFrame mainFrame = (JFrame) addCompnt(mainStr, mainLabel);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
} else {
System.out.println("Error");
}
} catch (FileNotFoundException fnfe) {
System.out.println("File not found");
fnfe.toString();
} catch (Exception unk) {
System.out.println("Unknown Error");
unk.toString();
}
}
private static ArrayList<Integer> getArray(String arrayStr) throws Exception {
int x, y;
ArrayList<Integer> arrayInt = new ArrayList<Integer>();
for (x = 0; x < arrayStr.length(); x ) {
for (y = x; y < arrayStr.length() && Character.isDigit(arrayStr.charAt(y)); y );
if (x != y) {
arrayInt.add(Integer.parseInt(arrayStr.substring(x, y)));
}
x = y;
}
return arrayInt;
}
private static Component addCompnt(String compntStr, String compntLabel) throws Exception {
String inputStr;
if (compntLabel.equalsIgnoreCase("Window")) {// gui ::= Window STRING
compntStr = compntStr.trim();
JFrame wndwFrame;
if (compntStr.charAt(0) == '"') {
compntStr = compntStr.substring(1);
inputStr = compntStr.substring(0, compntStr.indexOf('"'));
wndwFrame = new JFrame(inputStr);
compntStr = compntStr.substring(compntStr.indexOf('"') 1).trim();
} else {
wndwFrame = new JFrame("Missing Window Title");
}
if (compntStr.charAt(0) == '(') {// gui ::= '(' NUMBER ',' NUMBER ')' layout widgets End '.'
inputStr = compntStr.substring(0, compntStr.indexOf(')') 1);
compntStr = compntStr.substring(inputStr.length()).trim();
ArrayList<Integer> intStr = getArray(inputStr);
if (intStr.size() == 2) {
wndwFrame.setSize(intStr.get(0), intStr.get(1));
}
}
inputStr = getLabel(compntStr);
compntStr = compntStr.substring(inputStr.length()).trim();
JPanel loPanel = new JPanel();
if (inputStr.equalsIgnoreCase("Layout")) {// layout ::= Layout layout_type ':'
inputStr = getLabel(compntStr);
compntStr = compntStr.substring(inputStr.length()).trim();
if (inputStr.equalsIgnoreCase("Flow")) {// layout_type ::= Flow
FlowLayout fLayout = new FlowLayout();
loPanel.setLayout(fLayout);
}
if (inputStr.equalsIgnoreCase("Grid")) {// layout_type ::= Grid
if (compntStr.charAt(0) == '(') {
inputStr = compntStr.substring(0, compntStr.indexOf(')') 1);
compntStr = compntStr.substring(inputStr.length()).trim();
ArrayList<Integer> intStr = getArray(inputStr); // layout_type ::= NUMBER ',' NUMBER ',' NUMBER
GridLayout gLayout;
if (intStr.size() == 2) {
gLayout = new GridLayout(intStr.get(0), intStr.get(1));
loPanel.setLayout(gLayout);
} else if (intStr.size() == 4) {
gLayout = new GridLayout(intStr.get(0), intStr.get(1), intStr.get(2), intStr.get(3));
loPanel.setLayout(gLayout);
}
}
}
}
while (true) {
if (scan.hasNextLine()) {
compntStr = scan.nextLine().trim();
inputStr = getLabel(compntStr);
if (inputStr.equalsIgnoreCase("end")) {// gui ::= End '.'
break;
} else {
Component nestCompnt = addCompnt(compntStr.substring(inputStr.length()), inputStr);
if (nestCompnt != null) {
if (nestCompnt.getClass() == wndwFrame.getClass()) {
System.out.println("Window can't be nested inside");
} else {
loPanel.add(nestCompnt);
}
}
}
} else {
break;
}
}
wndwFrame.add(loPanel);
return wndwFrame;
}
if (compntLabel.equalsIgnoreCase("Button")) {// widget ::= Button STRING ';'
compntStr = compntStr.trim();
JButton widgetBttn;
if (compntStr.charAt(0) == '"') {
compntStr = compntStr.substring(1);
inputStr = compntStr.substring(0, compntStr.indexOf('\"'));
widgetBttn = new JButton(inputStr);
compntStr = compntStr.substring(compntStr.indexOf('"') 1).trim();
} else {
widgetBttn = new JButton("Missing Button Label");
}
return widgetBttn;
}
if (compntLabel.equalsIgnoreCase("Group")) {// widget ::= Group radio_button END ';'
compntStr = compntStr.trim();
JRadioButton grpBttn;
if (compntStr.charAt(0) == '"') {
compntStr = compntStr.substring(1);
inputStr = compntStr.substring(0, compntStr.indexOf('\"'));
grpBttn = new JRadioButton(inputStr);
compntStr = compntStr.substring(compntStr.indexOf('"') 1).trim();
} else {
grpBttn = new JRadioButton("Missing Group Label");
}
return grpBttn;
}
if (compntLabel.equalsIgnoreCase("Label")) {// widget ::= Label STRING ';'
compntStr = compntStr.trim();
JLabel widgetLabel;
if (compntStr.charAt(0) == '"') {
compntStr = compntStr.substring(1);
inputStr = compntStr.substring(0, compntStr.indexOf('\"'));
widgetLabel = new JLabel(inputStr);
compntStr = compntStr.substring(compntStr.indexOf('"') 1).trim();
} else {
widgetLabel = new JLabel("Missing Label");
}
return widgetLabel;
}
if (compntLabel.equalsIgnoreCase("Panel")) {// widget ::= Panel layout widgets End ';'
compntStr = compntStr.trim();
JPanel compntPnl = new JPanel();
inputStr = getLabel(compntStr);
compntStr = compntStr.substring(inputStr.length()).trim();
if (inputStr.equalsIgnoreCase("Layout")) {
inputStr = getLabel(compntStr);
compntStr = compntStr.substring(inputStr.length()).trim();
if (inputStr.equalsIgnoreCase("Flow")) {// layout_type ::= Flow
FlowLayout panelFlow = new FlowLayout();
compntPnl.setLayout(panelFlow);
}
if (inputStr.equalsIgnoreCase("Grid")) {// layout_type ::= Grid
if (compntStr.charAt(0) == '(') {
inputStr = compntStr.substring(0, compntStr.indexOf(')') 1);
compntStr = compntStr.substring(inputStr.length()).trim();
ArrayList<Integer> gridStrg = getArray(inputStr);
GridLayout gLayout;
if (gridStrg.size() == 2) {
gLayout = new GridLayout(gridStrg.get(0), gridStrg.get(1));
compntPnl.setLayout(gLayout);
} else if (gridStrg.size() == 4) {
gLayout = new GridLayout(gridStrg.get(0), gridStrg.get(1), gridStrg.get(2),
gridStrg.get(3));
compntPnl.setLayout(gLayout);
}
}
}
}
while (true) {
if (scan.hasNextLine()) {
compntStr = scan.nextLine().trim();
inputStr = getLabel(compntStr);
if (inputStr.equalsIgnoreCase("End")) {
break;
} else {
Component errCompnt = addCompnt(compntStr.substring(inputStr.length()), inputStr);
if (errCompnt != null) {
if (errCompnt.getClass() == new JFrame().getClass()) {
System.out.println("Panel Layout Error");
} else {
compntPnl.add(errCompnt);
}
}
}
} else {
System.out.println("Error in Panel Layout Nesting");
break;
}
}
return compntPnl;
}
if (compntLabel.equalsIgnoreCase("Textfield")) {// widget ::= Textfield NUMBER ';'
compntStr = compntStr.trim();
ArrayList<Integer> textStr = getArray(compntStr);
JTextField txtFld = new JTextField(textStr.get(0));
return txtFld;
}
if (compntLabel.equalsIgnoreCase("Radio")) {// widget ::= Radio STRING ';'
compntStr = compntStr.trim();
JRadioButton rdoBttn;
if (compntStr.charAt(0) == '"') {
compntStr = compntStr.substring(1);
inputStr = compntStr.substring(0, compntStr.indexOf('\"'));
rdoBttn = new JRadioButton(inputStr);
compntStr = compntStr.substring(compntStr.indexOf('"') 1).trim();
} else {
rdoBttn = new JRadioButton("Missing Radio Label");
}
return rdoBttn;
}
return null;
}
}
uj5u.com熱心網友回復:
你可以試試這個:
switch(compntLabel.getText().toUpperCase()) { /* your conditions */}
uj5u.com熱心網友回復:
你的意思是這樣的嗎?:
if (compntLabel.equalsIgnoreCase("Button") || compntLabel.equalsIgnoreCase("Group") || compntLabel.equalsIgnoreCase("Label")) {
// .. Common code
}
uj5u.com熱心網友回復:
你可以使用這個:
if(compntLabel.equalsIgnoreCase("Button") || compntLabel.equalsIgnoreCase("Group") || compntLabel.equalsIgnoreCase("Label"))
{
.
.
.
}
uj5u.com熱心網友回復:
在當前類中添加以下代碼。
static String matches[] = {
"Button",
"Group",
"Label"
};
static boolean checkMatch(String component) {
for( String m : matches)
if(component.equals(m))
return true;
return false;
}
并撰寫以下代碼來完成您的作業。
if(checkMatch(compntLabel)) {
// your code
}
無需撰寫奇怪的條件來檢查其中一個是否為真,您可以將所有邏輯放在一個方法中,該方法將相應地回傳真或假。
在您的情況下,該方法嘗試所有可能的匹配項(按鈕、標簽、組,并且可以通過將字串添加到陣列匹配項中輕松擴展),如果其中一個匹配引數,則回傳 true,如果沒有匹配則回傳 false。
注意:可能不需要 static 關鍵字,這取決于您撰寫代碼的類是否是靜態的。
uj5u.com熱心網友回復:
您可以創建一個Set并用您想要OR覆寫的值填充它,然后檢查它是否包含您的containerLabel.
if (Set.of("Button", "Group", "Label").contains(compntLabel)) { ... }
uj5u.com熱心網友回復:
由于我沒有您的完整代碼,因此我無法保證下面的代碼將運行;但是,我可以使用下面的代碼來解決您的一些問題,并為您提供一些可以幫助您改進代碼的想法。
所以,首先讓我們回顧一下我修改的整個代碼,然后我將回顧每個部分來解釋我為什么要這樣做:
private static final BUTTON_COMPONENT = "button";
private static final GROUP_COMPONENT = "group";
private static final LABEL_COMPONENT = "label";
private String compntStr = "something bla bla bla";
private JComponent doSomething(){
JComponent component = null;
switch(comnptLabel.toLowerCase()){
case BUTTON_COMPONENT:
component = doSomethingElseWithButton(getButton(compntStr));
break;
case GROUP_COMPONENT:
component = getGroup(compntStr);
break;
case LABEL_COMPONENT:
component = getLabel(compntStr);
break;
default:
break;
}
compntStr = postProcessString(compntStr);
return component;
}
private JButton getButton(String compntStr){
if (doStartWithQuote(compntStr)){
return getElement(compntStr);
}
return new T("Missing Button Label");
}
private boolean doStartWithQuote(String compntStr){
compntStr = compntStr.trim();
return compntStr.charAt(0) == '"';
}
private <T extends JComponent> T getElement(String compntStr){
return new T(preProcessString(compntStr));
}
private String preProcessString(String compntStr){
compntStr = compntStr.substring(1);
return compntStr.substring(0, compntStr.indexOf('\"'));
}
private JRadioButton getButton(String compntStr){
if (doStartWithQuote(compntStr)){
return getElement(compntStr);
}
return new T("Missing Group Label");
}
private JButton getButton(String compntStr){
if (doStartWithQuote(compntStr)){
return getElement(compntStr);
}
return new T("Missing Label");
}
private String postProcessString(String compntStr){
return compntStr.substring(compntStr.indexOf('"') 1).trim();
}
讓我們從您對使用 if 陳述句等感興趣的部分開始。
使用這種線的問題compntLabel.equalsIgnoreCase("Button")是它所涉及的處理量。在不深入細節的情況下,我認為如果您將 中的所有字符都小寫compntLabel,那么您可以與小寫字串進行比較,從而允許您使用 aswitch而不是if陳述句。
private JComponent doSomething(){
JComponent component = null;
switch(comnptLabel.toLowerCase()){
case "button":
component = doSomethingWithButton(getButton(compntStr));
break;
case "group":
component = getGroup(compntStr);
break;
case "label":
component = getLabel(compntStr);
break;
default:
break;
}
compntStr = postProcessString(compntStr);
return component;
}
接下來,讓我們將代碼分解成小的可重用方法:
private JButton getButton(String compntStr){
if (doStartWithQuote(compntStr)){
return getElement(compntStr);
}
return new T("Missing Button Label");
}
private boolean doStartWithQuote(String compntStr){
compntStr = compntStr.trim();
return compntStr.charAt(0) == '"';
}
private <T extends JComponent> T getElement(String compntStr){
inputStr = preProcessString(compntStr);
return new T(inputStr);
}
private String preProcessString(String compntStr){
compntStr = compntStr.substring(1);
return compntStr.substring(0, compntStr.indexOf('\"'));
}
我在這里使用通用方法,因為getElement它執行相同的操作,但回傳不同的型別,例如JButton, JRadioButton,以及JLabel哪個父類是JComponent.
private <T extends JComponent> T getElement(String compntStr){
T widgetBttn;
inputStr = preProcessString(compntStr);
return new T(inputStr);
}
此外,我們正在減少重復代碼并將它們放入我們提供有意義的名稱的方法中,從而使我們不必撰寫注釋。
private boolean doStartWithQuote(String compntStr){
compntStr = compntStr.trim();
return compntStr.charAt(0) == '"';
}
注釋的問題在于,如果其他開發人員更改了代碼但不更新注釋,則可能會造成混亂。但是,如果我將那一小段代碼放入一個方法中并提供一個描述性的名稱,就可以避免混淆并使代碼更易于閱讀。另外,如果出現問題,我們將收到一個運行時錯誤,指出問題所在。通過這種方式,我們更容易定位和解決問題。
最后,使用這種方法可以讓我們更輕松地添加新的小部件組件:
private JRadioButton getButton(String compntStr){
if (doStartWithQuote(compntStr)){
return getElement(compntStr);
}
return new T("Missing Group Label");
}
private JButton getButton(String compntStr){
if (doStartWithQuote(compntStr)){
return getElement(compntStr);
}
return new T("Missing Label");
}
另外,請注意,對于我呼叫的每個函式,我都嘗試將它放在呼叫它的方法的正下方。這使得從上到下閱讀更容易。
其中許多更改都基于 Robert C. Martin aka 的“清潔代碼”一書。鮑伯叔叔。
我希望這對你有所幫助。如果沒有,請告訴我,我可以洗掉這個答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463106.html
