Can't get java program to work correctly.

Mac

Newbie
Joined
Jul 30, 2003
Messages
778
Reaction score
0
I'm a fairly new programmer, taking my first college level programming class this semester. Its in java.

one of the projects to do is to:
Design and implement an application that prints the first few verses of the traveling song "One Hundred Bottles of Beer." Use a loop such that each iteration prints one verse. Read the number of verses to print from the user. Validate the input. The following is the first verse of the song:
100 bottles of beer on the wall
100 bottles of beer
If one of those bottles should happen to fall
99 bottles of beer on the wall.
At the end of each printing of the verses, prompt to determine if the users wants to do it again.

so far this is what i have, it is almost complete except a small problem.

Code:
//**************************************************
// BeerCount.java	    By: Matt LaDuca
//
// Inputs the number of lines of the "one hundred 
// bottles of beer" song to be printed
//**************************************************

import java.util.Scanner;

public class BeerCount
{
    public static void main(String[] args)
    {
	int    numOfLines;
	String another = "y";
	
	Scanner scan = new Scanner(System.in);
	
	while(another.equals("y"))
	{
	    System.out.print("\nEnter the number of verses of the Beer song to be printed: ");
	    numOfLines = scan.nextInt();
	
	    while(numOfLines < 0 || numOfLines > 100)
	    {
	        System.out.print("Invalid input. Please enter a number between 0 and 100 (inclusive): ");
	        numOfLines = scan.nextInt();
	    }
	
	    for(int i = 100; i > 100 - numOfLines; i--)
	    {
	        System.out.println("\n" + i + " bottles of beer on the wall");
	        System.out.println("\n" + i + " bottles of beer");
	        System.out.println("\nIf one of those bottles should happen to fall");
	        System.out.println("\n" + (i - 1) + " bottles of beer on the wall");
	    }
	    
	    System.out.print("Again? (y/n): ");
	    another = scan.nextLine();	    
	  
	}   
    }
}

for some reason, it gets to the "Again?(y/n): " but it doesnt allow the user to enter any input. it just quits. I dont get how it's exiting the main while loop. There must be something im missing, but i dont see it.

here's what the output looks like:
C:\Program Files\Java\LaDuca>java BeerCount


Enter the number of verses of the Beer song to be printed (0 to quit): 1

100 bottles of beer on the wall

100 bottles of beer

If one of those bottles should happen to fall

99 bottles of beer on the wall
Again? (y/n):
C:\Program Files\Java\LaDuca>

any help is appreciated. sorry for my noobness.
 
I don't know java, but my guess it's alrady taking an input or something .. try to add mutiple "another = scan.nextLine();"
Code:
System.out.print("Again? (y/n): ");
another = scan.nextLine();
another = scan.nextLine();

or maybe you need to clear the input buffer before taking input?
 
I'll be damned, it worked...

I'm still confused as hell though.

there is a program just like this in the book and it doesnt need two 'scan.nextLine' lines.. and it doesn't clear the input buffer either..
 
this program works just fine without two 'another = scan.nextLine' lines..

Code:
import java.util.Scanner;

public class PalindromeTester
{
   //-----------------------------------------------------------------
   //  Tests strings to see if they are palindromes.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      String str, another = "y";
      int left, right;

      Scanner scan = new Scanner (System.in);

      while (another.equalsIgnoreCase("y")) // allows y or Y
      {
         System.out.println ("Enter a potential palindrome:");
         str = scan.nextLine();

         left = 0;
         right = str.length() - 1;

         while (str.charAt(left) == str.charAt(right) && left < right)
         {
            left++;
            right--;
         }

         System.out.println();

         if (left < right)
            System.out.println ("That string is NOT a palindrome.");
         else
            System.out.println ("That string IS a palindrome.");

         System.out.println();
         System.out.print ("Test another palindrome (y/n)? ");
         another = scan.nextLine();
      }
   }
}

i dont see how this one works and mine (BeerCount.java) doesn't

maybe someone else can see it
 
I think because in that program you have a nextLine then a nextLine.
in your case it's a nextInt then a nextLine.
This is just my guess, but when you input an integer and press enter, you essentially entered two things: an int then a newline character.
nextInt looks for an integer in the buffer, when it finds it, it stops there.
nextLine will look for characters (my guess), it finds a newline (still in the buffer from the last input), and takes it as an input. so it doesn't let the user input anything cuz it doesn't need anything.
to make sure, try to display the ascii of first character of the input before the second input (or if you can see that through some debug options), and see what it is.
 
ahh i get it, thanks.

I guess ill just use two scan.nextLine()'s from now on if it comes after a scan.nextInt.
 
I guess you need to clear the input buffer after you first ask for a response from the user, after you call scan.nextInt, discarding the newline char which is left there.

When you ask for input the seccond time, it continues to read from the buffer where it left off, at the newline char (return), exiting before you can reply.

Im not sure how you'd go about this in java though :p

Maybe you could use scan.nextLine(nextInt) or read the entire line into a variable, then check that for your Int input. Leaving the input buffer empty and ready for the seccond response?

(something like that anyway, just slap me if im full of crap :p)
 
you don'thave to use two nextlines, it's rpobably better to clear the buffer before you do a nextline, although I don't know how to do it in java, but it has to be something like scan.clear()
 
Does it make a difference what kind of Java platform it runs on, I know its all "supposed" to be the same but I've found the odd misc app that doesn't like running on one or the other for some reason.
 
hasan said:
you don'thave to use two nextlines, it's rpobably better to clear the buffer before you do a nextline, although I don't know how to do it in java, but it has to be something like scan.clear()

hmm, I've tried looking for this in the java class library, didnt find any like it, at least not in the scanner class.
 
The Dark Elf said:
Does it make a difference what kind of Java platform it runs on, I know its all "supposed" to be the same but I've found the odd misc app that doesn't like running on one or the other for some reason.

hmm, im using java 1.5 beta 2 i think. not sure what you mean by 'platform'.
 
You could just read the whole line into a String then parse the string to an integer...
 
Back
Top