While(str1 == length)
Explanation:
There are multiple reasons why the line While(str1 == length)
results in errors.
While
is not the correct syntax, while
with a lowercase w
is the correct syntax.
str1
is a String
, String
is an Object
, when checking for equality of objects through the operator ==
, the references (locations in memory) are checked, not the contents of the objects. To check for equality between objects use the .equals()
method.
Note: Using the ==
operator between objects will not result in a compilation error. But if used to check for object equality then it will result in a logical error, which is usually way worse than a compilation error.
You never declared a variable length
, I'm assuming you meant to write the loop's condition to resemble while(str1.length() != 0)
. If you did mean to write while(str1 == length)
, assuming length
is an int
, it does not make since to compare an object (String
) to a primitive (int
) and it's not allowed.
str1++
Explanation:
The reason str1++;
results in error is similar to the second point above. str1
is a String
which is an Object
. Most primitives (int
, float
, double
, ...) have +, -, *, /, ++, --, ** ...
operators, but most objects don't, with the exception of a few like Iterator
s, and the objects that do are set in stone. Languages like C++ allow you to overload operators but Java does not. Thus, str1++
does not work, you have to use the methods supplied by the String
class to alter the contents of str1
.
frame.add(tf)
Explanation:
The reason frame.add(tf)
results in error is because tf
is no longer in scope. tf
was declared inside the while
loop, anything declared inside curly braces ({ }
) cannot be referenced outside of the curly braces. If you need to alter a variable within curly braces and then use it outside of curly braces, then declare it before the curly braces.
Future Logical Error:
The assignment requires a user to enter a sentence.
If the words in the sentence are separated by spaces then you're going to wonder why only the first word in the sentence is being processed. The reason lies in the difference between the next
and nextLine
methods of the Scanner
class. next
reads until a specified delimiter is encountered (space by default), nextLine
reads until the operating system's new-line character is encountered. Thus you're going to want to use:
str1 = sc.nextLine();
Here's a good StackOverflow question with answers, in case you run into any trouble while using the Scanner
class' next
methods: Skipping nextLine() after using next(), nextInt() or other nextFoo() methods
Side Note:
You can use a while
loop to achieve the desired results, but a for
loop is much easier to implement in this situation. If you use a for
loop then you won't have to truncate str1
on every iteration. A for
loop example:
// 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);
}
However, if you have to use a while
loop then something similar to the following will work:
// 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);
}
Working main
Method Code:
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);
}