Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

checking textfields for a stream  RSS feed

 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having a little problem checking textfields to make sure that they are valid (filled in) for a data stream. the first thing I did was converted the textfields to strings and then created an if statement to check to make sure that there isn't a "" in the field. (there should never be a blank space anyway). Here is what I have done. any suggestions on how to fix this would be great.
if(wOrder == ""){
fieldMessage FMG = new fieldMessage();
workOrder.requestFocus();
}
if(qnty == ""){
fieldMessage FMG = new fieldMessage();
}
if(loc ==""){
fieldMessage FMG = new fieldMessage();
}
if(emp ==""){
fieldMessage FMG = new fieldMessage();
}
if(prt==""){
fieldMessage FMG = new fieldMessage();
}
else{
try {
/** The output stream for the data. This is the layout of the text is
* Date, Time, work order, Employee Number, Part, Quantity,"QW","3"
*,Location,"m"
*/
ostream.println
(year + month + date + "," + hour + ":" + minute + ":" + second + "," + wOrder + "," + emp + "," + prt + "," + qnty + "," + "QW" + "," + "3" + "," + loc + "," + "m");
//locks in the variables of the fields so they do not have to be entered
//a second time.
workOrder.setText(wOrder);
workOrder.setEditable(false);
workOrder.setBackground(Color.lightGray);

employee.setText(emp);
employee.setEditable(false);
employee.setBackground(Color.lightGray);

location.setText(loc);
location.setEditable(false);
location.setBackground(Color.lightGray);

part.setText("");
part.requestFocus();

quantity.setText("");

oTLabel.setText(prt);
}
catch(NumberFormatException e2) {
System.err.println("Invalid number of part");
}
catch(Exception e3) {
System.err.println("Error writing file");
System.exit(1);
}
}

thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you haven't told us what the problem you're having actually is, so I have to guess. I see two fairly important problems in the code, and maybe that will help you. The first is that you're using == to compare strings; in Java, the equals operator checks that two variables refer to the same String object, not that they have the same contents; i.e., the value of
"" == new String("")
is false, because there are two different objects. You should use the equals() method instead:
"".equals(new String(""))
is true.
Now, for the second problem: there are a bunch of "if" statements at the beginning, followed by one "else". The else at the end will only be taken if the test on prt fails; it doesn't depend at all on the tests for all the other fields, but somehow it looks to me as if you think it should. I can't really understand what you're trying to do with this code, so I can't give you an alternative, but somehow the logic seems wrong to me.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about that...
what I'm trying to do is check a bunch of textfields for a void entry and if there is nothing entered then have a display screen telling them to fill in the text field. Then if all of the text fields are filled in then process the data into an output stream.
does that help to make more sense?
thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, well, given my comments, do you see some things you could do to fix the code? If you fix it up according to these suggestions, what problems do you havr then?
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that worked great this is what I changed it to.....
if(wOrder.equals(new String(""))){
fieldMessage FMG = new fieldMessage();
workOrder.requestFocus();
}
else if(qnty.equals(new String(""))){
fieldMessage FMG = new fieldMessage();
}
else if(loc.equals(new String(""))){
fieldMessage FMG = new fieldMessage();
}
else if(emp.equals(new String(""))){
fieldMessage FMG = new fieldMessage();
}
else if(prt.equals(new String(""))){
fieldMessage FMG = new fieldMessage();
}
else{
Does this look more logical to you now?
 
Tiberiu Paun
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but what u r trying there is not cool.
First, the String "" should be declared final somewhere, and not instantiate it everytime.
Secondly, if u want to not be dependant of how many fields must be there, I would say u should take this approach:
final String empty="";
....
Component[] comps=YourFrame.getComponents();
for(int i=0;i<comps.length;i++){
if(comps instanceof TextField //or whatever class TextMessage, whatever u use){
TextField field=(TextField)comps;
if(field.getText().equals(empty)){//whatever want u to do}
}
}
The array of the TextFields u put in the frame can be set when u add them to the Frame.
Hope that answers to your question
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the thing is not all of the textfields need to be checked...
this will be done several times with different textfields lets say three of five, why would I want to build an array when the if statement would work? all I want to to display a window that the information needs to be entered.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your new version of the code is definitely an improvement. You don't need to do "new String("")" -- you must've got the wrong impression from my earlier message. Just a bare "" will do fine -- i.e.,
... else if(prt.equals("")){
I don't see any reason to declare a variable "empty"; I think "" is clearer, anyway. As for the loop, again I don't think it's necessary. And as for the generic loop recommended above, obviously it's dangerous; later changes in the GUI could break it altogether.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help everything works great now!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!