• 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

Accessing Array

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

For this program the user enters the number of indexes for an array, and then enters the information for each index. Then, it displays the elements for those indexes back to the user. I cannot get the 'display back to the user'. I've defined everything outside of my while loop, so I cannot figure out where things are going wrong. Thank you in advance for any help . Here is my code:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exact problem are you having?
 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

The problem is the program will not display the contents of my cityArray outside of the while loop. So the while loop runs and user enters in data for each index...everything is fine. Then (where I note in the code above) I try to print that information to the screen...and nada...no error message...literally nothing prints to the screen. This is the kicker though, if instead of trying to print the contents of my array, I just try to print something like "Hello" it prints to the screen just fine.



 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, this does not print anything to my screen (but of course I want it to):



but this prints to my screen just fine:

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to initialize numElements variable where you declare it to some value (say 2) and remove the line where you are reading user's input for number of elements. Leave the rest of the code as it is and see if does the job. If that's the case, then you'll at least know where the problem is.

I bet scan.nextInt() is making a problem, since there is escape character left that is picked later when you are getting other input(s) from the user. Similar problem has already been resolved here on the forum couple of weeks ago.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I suggest, even if you now know what the problem is: Every time you call one of Scanner's "next" methods, print out its results, and an indication of where you are in the code. In the case where you're currently ignoring it, add a dummy variable:


This will help you spot some of your erroneous assumptions about how Scanner works. The square brackets will help you spot whitespace.

This is a technique you'll want to hang on to--printing out variables and method call results to see which ones don't match your assumptions, and what they are instead.
 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Kemal you are exactly correct. First I did as you suggested and set my numElements to a constant and everything ran as intended...so found my problem. It was .nextInt(). I simply followed .nextInt() immediately with .nextLine(), which I guess flushed out or cleared that extra character. Now the program works as I originally intended...user can enter number of indexes, etc.

Thanks for the tip Jeff...that is super nifty (for a noob like myself). I'm going to make it habit to do that for all my code from now on...it only takes an extra second and looks like it can save a lot of time and headache if I'm trying to trace down a bug.

Thanks to both of you for your help
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.

Thanks for the tip Jeff...that is super nifty (for a noob like myself). I'm going to make it habit to do that for all my code from now on...it only takes an extra second and looks like it can save a lot of time and headache if I'm trying to trace down a bug.


The tip Mr Verdegan gave you is the one I also picked up from him on this forum, and it really saves a lot of time when it comes to buggy code (combined with debugger if necessary, it can really save you a lot of time). So make sure you keep that one in mind.
 
Ranch Hand
Posts: 91
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two problems with your code.


First, why are you inserting the line scan.next()? Think is it necessary? Read the JDK API. For this method.

And second, you should iterate over the number of cities according to numElements value and print the city name.


Edit these two lines.

If you still have questions feel free to post the question here.

Hope it helps.

Thank you.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic