• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JList problem

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having problems reading from a JList, my application consists in adding text line by line from a text file to a JList, ..and the problem that i am facing is...how can i read elements from Jlist, character by character .... help please....
 
axel axek
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the code that reads form txt:
where "model" is Jlist model

and i have tried to read from list:
the line i'm tryng to read is something like: "S LLLL 0.2"
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Because you are trying to access a character in the string that doesn't exist. I don't know the format of your data, but maybe you have a blank line.

In any case you should be using the String.length() method to control the looping.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to say it is a bit difficult to follow your code as it doesn't seem to be complete (for instance where do the for loops for k and j finish?).
But looking at the code as it stands and at the error message it is basically saying that you are trying to reference a part of the string ('s1') that does not exist. You should be able to see this if you output the length of the string 's1' and the value of k where it fails (you'll probably find that k >= s1.length()).

<hint>Remember that String.charAt(int) references the string as it would a list so characters are retreived from position 0 rather than 1.</hint>

Also not sure if you have been asked to use for loops for your project but if not you may want to have a look in using StringTokenizer, this should take a lot of the pain out of what you are trying to do.

Have fun...
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic