File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Accessing Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing Array" Watch "Accessing Array" New topic
Author

Accessing Array

Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
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:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What exact problem are you having?
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
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

Joined: Oct 22, 2012
Posts: 37
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:

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 22, 2012
Posts: 37
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Array