This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am new in java programming still need to learn more. I did try searching and reading some of the topics here and it was really helpful for a beginner like me. Currently, I'm trying to create a Address Book that will store a name and an address in the array and somehow my little program is doing it. when I try to use the AddModule(); and then use the deleteModule(); it will do what I wish but after that when I try to use the AddModule() again, It supposed to get input of the name but my program skip it and jump to the address. Can someone help me? your comment will be very much appreciated.
My guess is that the problem is with the console (in.next(), in.nextLine(), etc.). If you're using a number-based menu system (e.g. type 1 to add an address, type 2 to delete, etc.) then some of the methods you use to read that data from the console may be leaving linefeeds around. For instance, if it skips the prompt for name, it may be because there's already a linefeed that didn't get read. (This could happen if you use nextInt() from the menu.)
I usually use nextLine() for everything. This way I'm sure the linefeed is handled. If you need to get an integer out of it, you could use something like this in the menu:
int menuOption = Integer.parseInt(in.nextLine());
I'm interested to know if this works for you. Take care, :-)
I disagree. I don’t think always using nextLine() is the correct way to use a Scanner. You should use nextInt() etc. But you are right about line end characters. They can cause no end of confusion to Scanner users, because people think nextLine() goes onto the next line, which it doesn’t. I see it is now over 1½ years since I posted this, but it explains about nextLine(). You may need to call nextLine() and discard its result before using nextLine() for a real result.
By the way: A Scanner can throw an InputMismatchException from nextInt() and similar methods. Here you can see a method in a utility class (which you would do well to write for yourself and reuse later) which can avoid such an exception
Joined: Apr 04, 2012
I'm sorry if It took me a week to get back.
Hi Charlie Schwartz,
Your suggestion really help me out by parsing the string into a integer and have a newline. I forgot that the nextInt() will not have a new line and it confused me. I thought there is something wrong in my addModule(). I was also surprise that your guess was right. I am using a number-base system menu.
Hi Campbell Ritchie,
the links was very helpful! The CD class is very similar to what my program is doing. I was also enlighten on how I could use the constructors instead of using a setter method. And then I think is setter method is enough or good practice in programming or do constructor is way better than setter method? I wanted to know, What is prefered by most of the java practitioner?