[英]Java while-loop create text fields from string characters
我目前正在處理文本字段,而我正在使用帶有Java的NetBeans 。 作業要求用戶輸入句子。 程序讀取句子,並顯示一個框架,其中包含用戶輸入的句子,但每個字符都在其自己的文本字段中。 下面是我的代碼以及一些注釋,指出我在哪里遇到錯誤。
package lab.pkg2.pkg2;
import java.awt.FlowLayout;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Lab22 {
public static void main(String[] args) {
// initialize string
String str1;
Scanner sc = new Scanner(System.in); // scanner reads string
System.out.print("Enter a sentence");
str1 = sc.next();
System.out.println(str1);
// Create frame
JFrame frame = new JFrame("Characters in Text field :");
frame.setLayout(new FlowLayout());
// Create a text field with a single character
While(str1 == length) { // error
JTextField tf = new JTextField(4);
System.out.print(str1.length());
String ch = str1;
tf.setText(String.valueOf(ch));
str1++; // error
}
String ch = str1;
frame.add(tf); // error
// make another text field with a single character
// Set up the frame and displays it
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
While(str1 == length)
說明: 線條While(str1 == length)
導致錯誤的原因有多種。
While
不是正確的語法, while
使用小寫w
是正確的語法。
str1
是一個String
, String
是一個Object
,當通過operator ==
檢查Object
是否相等時,會檢查引用(內存中的位置),而不是對象的內容。 要檢查對象之間的相等性,請使用.equals()
方法。
注意:在對象之間使用==
運算符不會導致編譯錯誤。 但是,如果用於檢查對象相等性,那么它將導致邏輯錯誤,這通常比編譯錯誤更糟糕。
你從來沒有聲明一個變量length
,我假設你打算把循環的條件寫成類似於while(str1.length() != 0)
。 如果你的意思是寫while(str1 == length)
,假設length
是一個int
,那么它就不會因為將一個對象( String
)與一個原語( int
)進行比較而不允許它。
str1++
說明: 原因str1++;
結果錯誤類似於上面的第二點。 str1
是一個String
,它是一個Object
。 大多數原語( int
, float
, double
,...)都有+, -, *, /, ++, --, ** ...
運算符,但大多數對象都沒有,除了像Iterator
這樣的一些s,並且做的對象是石頭。 像C ++這樣的語言允許你重載運算符,但Java不允許。 因此, str1++
不起作用,您必須使用String
類提供的方法來更改str1
的內容。
frame.add(tf)
說明: frame.add(tf)
導致錯誤的原因是因為tf
不再在范圍內。 在while
循環中聲明了tf
,在花括號( { }
)內聲明的任何東西都不能在花括號之外引用。 如果你需要在花括號內修改變量然后在花括號之外使用它,那么在花括號之前聲明它。
作業要求用戶輸入句子。
如果句子中的單詞用空格分隔,那么你就會想知道為什么只處理句子中的第一個單詞。 原因在於Scanner
類的next
和nextLine
方法之間的區別。 next
讀取直到遇到指定的分隔符(默認為空格), nextLine
讀取直到遇到操作系統的換行符。 因此,您將要使用:
str1 = sc.nextLine();
這是一個很好的StackOverflow問題和答案,以防你在使用Scanner
類的next
方法時遇到任何麻煩: 在使用next(),nextInt()或其他nextFoo()方法后跳過nextLine()
您可以使用while
循環來獲得所需的結果,但在這種情況下, for
循環更容易實現。 如果使用for
循環,則不必在每次迭代時截斷str1
。 一個for
循環的例子:
// create text fields with a single character
for(int i = 0; i < str1.length(); i++) {
JTextField tf = new JTextField(4);
char ch = str1.charAt(i);
// set the newly created text fields text to ch
tf.setText(ch + "");
// add the text field to frame while it's still in scope
frame.add(tf);
}
但是,如果你必須使用while
循環,那么類似於以下內容將起作用:
// create text fields with a single character
while(str1.length() != 0) {
JTextField tf = new JTextField(4);
char ch = str1.charAt(0);
// chop off first (zeroeth) character from str1
// unless it's the last character
str1 = (str1.length() > 1) ? str1.substring(1) : "";
// set the newly created text fields text to ch
tf.setText(ch + "");
// add the text field to frame while it's still in scope
frame.add(tf);
}
main
方法代碼: public static void main(String[] args) {
// initialize string
String str1;
Scanner sc = new Scanner(System.in); // scanner reads string
System.out.print("Enter a sentence");
str1 = sc.next();
System.out.println(str1);
// create and set up frame
JFrame frame = new JFrame("Characters in Text field :");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create text fields with a single character
for(int i = 0; i < str1.length(); i++) {
JTextField tf = new JTextField(4);
char ch = str1.charAt(i);
// output the character for debugging?
System.out.println(ch);
// set the newly created text fields text to ch
tf.setText(ch + "");
// add the text field to frame while it's still in scope
frame.add(tf);
}
// let frame's layout manager do it's thing
frame.pack();
// show the frame
frame.setVisible(true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.