Java while-loop create text fields from string characters -
i'm working on text fields, , i'm using netbeans java. assignment requires user enter sentence. program reads sentence, , displays frame sentence user entered, each character in own text field. below code along comments point out i'm getting errors.
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 sentence"); str1 = sc.next(); system.out.println(str1); // create frame jframe frame = new jframe("characters in text field :"); frame.setlayout(new flowlayout()); // create text field 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 text field single character // set frame , displays frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
while(str1 == length) explanation:
there multiple reasons why line while(str1 == length) results in errors.
whilenot correct syntax,whilelowercasewcorrect syntax.str1string,stringobject, when checking equality of objects through operator==, references (locations in memory) checked, not contents of objects. check equality between objects use.equals()method.
note: using==operator between objects not result in compilation error. if used check object equality result in logical error, way worse compilation error.you never declared variable
length, i'm assuming meant write loop's condition resemblewhile(str1.length() != 0). if did mean writewhile(str1 == length), assuminglengthint, not make since compare object (string) primitive (int) , it's not allowed.
str1++ explanation:
the reason str1++; results in error similar second point above. str1 string object. primitives (int, float, double, ...) have +, -, *, /, ++, --, ** ... operators, objects don't, exception of few iterators, , objects set in stone. languages c++ allow overload operators java not. thus, str1++ not work, have use methods supplied string class alter contents of str1.
frame.add(tf) explanation:
the reason frame.add(tf) results in error because tf no longer in scope. tf declared inside while loop, declared inside curly braces ({ }) cannot referenced outside of curly braces. if need alter variable within curly braces , use outside of curly braces, declare before curly braces.
future logical error:
the assignment requires user enter sentence.
if words in sentence separated spaces you're going wonder why first word in sentence being processed. reason lies in difference between next , nextline methods of scanner class. next reads until specified delimiter encountered (space default), nextline reads until operating system's new-line character encountered. you're going want use:
str1 = sc.nextline(); here's stackoverflow question answers, in case run trouble while using scanner class' next methods: skipping nextline() after using next(), nextint() or other nextfoo() methods
side note:
you can use while loop achieve desired results, for loop easier implement in situation. if use for loop won't have truncate str1 on every iteration. for loop example:
// create text fields single character for(int = 0; < str1.length(); i++) { jtextfield tf = new jtextfield(4); char ch = str1.charat(i); // set newly created text fields text ch tf.settext(ch + ""); // add text field frame while it's still in scope frame.add(tf); } however, if have use while loop similar following work:
// create text fields single character while(str1.length() != 0) { jtextfield tf = new jtextfield(4); char ch = str1.charat(0); // chop off first (zeroeth) character str1 // unless it's last character str1 = (str1.length() > 1) ? str1.substring(1) : ""; // set newly created text fields text ch tf.settext(ch + ""); // add text field 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 sentence"); str1 = sc.next(); system.out.println(str1); // create , set frame jframe frame = new jframe("characters in text field :"); frame.setlayout(new flowlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); // create text fields single character for(int = 0; < str1.length(); i++) { jtextfield tf = new jtextfield(4); char ch = str1.charat(i); // output character debugging? system.out.println(ch); // set newly created text fields text ch tf.settext(ch + ""); // add text field frame while it's still in scope frame.add(tf); } // let frame's layout manager it's thing frame.pack(); // show frame frame.setvisible(true); }
Comments
Post a Comment