Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

if/else for comma count case

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am loading text file contents to GUI. The line is splitted by comma.
Now by counting commas in line I want to set them to jcb and textfields.
But when I use if/else for 2 cases that is commas<4 and commas<3, I found that jcb and textfields are interfere each other.
In case of when commas==3, jTextField1 getting the value of t1[1] except t1[2].
How can I write if/else for this case without interfere them?

 
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm very confused about why that code is written that way.

First of all wouldn't it be better to count the commas first, if you've decided it's necessary to do that? As it is your for-loop executes the if-else block once for each character in the input string instead of just once. Maybe you put the closing brace way down at the end by accident.

Not that it really matters, the last execution of the if-else block will occur when the last character in the input string is processed, so the comma count will be correct then, but it's kind of pointless to do it the way you posted.

As for the if-else block itself, you've got the tests in the wrong order. If commas is equal to 3 then it's less than 4, right? So which branch is executed?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tai Yo wrote:I am loading text file contents to GUI. The line is splitted by comma.
Now by counting commas in line I want to set them to jcb and textfields.


Those two statements are contradictory.

If you've just split() your string by commas (which you have), there will no longer be any commas in the resulting strings to count.
However, there is a very simple way of "counting commas". Viz:Job done.

As for the other stuff that Paul spoke about, I agree with him; but I'll let him handle it.

HIH

Winston
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for response. I solved it using switch statement.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic