Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Arrays with files

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a program to display the array data n find the largest n smallest values.

I am getting an exception in the main program as java.lang.NumberFormatException :For input String :"2 3 4".

Please tell me where I am wrong. I feel my getTable method is the problem.
Please help me out it is really tough I always get stuck at using indexOf n substring.

debug.dat :
4 4
1 2 3 4
5 6 7 8
9 8 7 6
1 3 3 5





 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prerna,

Instead of using substring you can try using:

String [] strArray = line.split(" ");

and then just loop through the values in the row.

Hope that helps,

Aly
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having your constructor take the dimensions of your array requires you to know what is inside your file. However, since the program is going to open the file and read these dimensions anyway, just let the program figure it out. All you need to know then is the filepath to the file and your program will do the rest. Thus I modified the code to take advantage of this. Also, as Aly had said using the split method from the String class is much simpler.



Then you can call your printTable(), smallest() and largest() methods. Here's how I called it :


and the output:



The first four lines are from the println() on line 28. I was using that to debug and forgot to remove it. That should be removed
 
Marshal
Posts: 75686
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch D Burks
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic