Results 1 to 12 of 12
  1. #1
    Kangaroo128 is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default Problem with scanner

    Hey guys/gals. I'm having trouble with a project that is supposed to take input from the user in the form of integers between 0 and 20, and print out a triangle in the form of:

    *****
    ****
    ***
    **
    *

    (if the user had entered the number '5' for example--with 5 being the top line.)

    I'm currently getting an error where once the program is started there is no prompt for the user, but if you enter a valid integer (like '5' again), you'll get the result.

    Enter a number <=20 (or 'quit'): *****
    ****
    ***
    **
    *

    With the first line of stars printing out beside the prompt where it should print:

    Enter a number <=20 (or 'quit'): 5
    *****
    etc.

    Any help is appreciated. Here is the code:

    Java Code:
    [B]import java.util.Scanner;
    
    public class Triangle
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            
            while(!input.hasNext("quit"))
            { 
                System.out.print("Enter a number <=20 (or 'quit'): ");
                int x = input.nextInt();
                
                if(x <= 20 && x > 0 )
                {    
                    for(int i=x; i>0 ; i--)
                    {
                        for(int j=i; j>0; j--)
                        {
                            System.out.print("*");
                        }
                        System.out.println("");
                    }
                }
                else
                {
                    System.out.println("Please enter a valid number.");
                }    
            
            }
            System.out.println("Bye");
        }
    }[/B]
    Last edited by Kangaroo128; 08-29-2009 at 09:25 PM.

  2. #2
    Kangaroo128 is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    I apologize for the lack of indentation, I'm new here and I didn't know if there was a way to maintain the formatting. :o

    EDIT: Nevermind...I figured out the indentation. That was an easy fix!
    Last edited by Kangaroo128; 08-29-2009 at 09:26 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    Scanner input = new Scanner(System.in);
    while(!input.hasNext("quit"))
    { 
        System.out.print("Enter a number <=20 (or 'quit'): ");
        ...
    Think about what those three lines do:

    (1) Create a scanner
    (2) Check that the next thing entered does not match "quit" and if it does not...
    (3) Print a prompt
    (4) do stuff and return to (2)

    At the moment the scanner sits and waits to see what the next input is when you would really like to move on and print the prompt. You will have to rethink this.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I should have added (since I didn't supply a jot of code) that it might be a good idea to say where you're "at" with Java, and whether there are any special constraints:

    ? Does it have to be "quit"? Having the user enter a number <=0 would make life easier.
    ? Have you come across the Integer.parseInt() method?
    ? do loops?

    In any event hasNext("whatever") will want to check the subsequent input, so the the (or a) prompt must come before this expression.

  5. #5
    Kangaroo128 is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    I should have added (since I didn't supply a jot of code) that it might be a good idea to say where you're "at" with Java, and whether there are any special constraints:

    ? Does it have to be "quit"? Having the user enter a number <=0 would make life easier.
    ? Have you come across the Integer.parseInt() method?
    ? do loops?

    In any event hasNext("whatever") will want to check the subsequent input, so the the (or a) prompt must come before this expression.
    I appreciate the help. As far as where I'm at with Java, I'm a second year CS student and have only really had one serious semester of Java programming so far. This is the first week of class and this was my best shot at this project. This is the first time I've ever used the scanner and some other things in this project. I have dealt with loops before but mostly only when it comes to iterating through arrays.

    The project stipulates that the user has to enter an integer greater than 0 and less than or equal to 20, and also has to use "quit" if they want to exit. Also, the program is supposed to loop back and prompt the user for another integer to create another triangle and do this looping until the quit command is used. As you said, creating this project without "quit" wouldn't have been as hard for me. As a side note, I originally had while(!input.equals("quit")) and the triangle bit worked but the quit exiting the loop part did not. When I read the API, I realized that equals wasn't used for the scanner and I changed it to hasNext. That fixed the quit part exiting the loop, but broke the triangle section. Ugh. I'll go look at it and see what I can do. Thanks again! :)
    Last edited by Kangaroo128; 08-30-2009 at 12:22 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I wonder a bit about the condition in the while loop. Basically the loop is meant to just go forever - with the "quit" input being something of an exception. Oe way of coding this would be:

    Java Code:
    while(true)
    {
        //...
        if(???)
        {
            break;
        }
        // ...
    }
    The break gets us out of the loop when some condition - yet to be figured out, but involving the user entering "quit" - is true. Until that condition becomes true we go round and round the loop forever.

    I hope I'm not giving too much away, but the following pseudo code attempts to put the prompt where it belongs: before the input. And also it reads the input as a string, rather than as a integer. The reason for that is that we want to check whether the user has entered a numeral (like "42") or a nonnumeric string (specifically, "quit").

    Java Code:
    FOREVER
        prompt the user
        get their response as a string
        IF the input string is "quit" THEN BREAK
        convert the input string to a number
        IF the number is bad 
        THEN 
            give the user a message
            CONTINUE
        draw the triangle
    END of forever
    Scanner has a method nextLine() which you should check out. It returns what the user entered as a String. You may well find that this method handles new lines more nicely than just using next().

    Integer has a (static) method parseInt() that will do the work of converting the user input String into an integer number.

    You may well want to read up about the "continue" and "break" keywords, how they relate to while loops to see why "continue" is appropriate when the user enters an out of range number.
    Last edited by pbrockway2; 08-30-2009 at 01:05 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    And - if you have any say in this - the actual work of drawing the triangle should go in a separate method as main() is already quite long enough just dealing with the user's input.

  8. #8
    Kangaroo128 is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    We actually are allowed to break it up into several methods if we want. Just one quick question before I stop working on this for tonight, when you write multiple methods and you are also using the main method, where do you put the methods? Up until now I've always worked in IDE's so I don't know the conventions for using multiple methods with main. Again thanks for all the help and I'll try to get this thing ironed out tomorrow.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    import java.util.Scanner;
    
    public class Triangle
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
    
            while(true)
            {
                // revised code for user input goes here (including break/continue
                // the step "convert the input string into a number" will
                // result in a variable you called "x"
    
                drawTriangle(x);
            }
            System.out.println("Bye");
        }
    
        private static void drawTriangle(int x)
        {
            for(int i=x; i>0 ; i--)
            {
                for(int j=i; j>0; j--)
                {
                    System.out.print("*");
                }
                System.out.println("");
            }
        }
    }

  10. #10
    Kangaroo128 is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    Ok cool, I had exactly that but it wasn't compiling because I had left out a "static". :rolleyes:

  11. #11
    Kangaroo128 is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    I finally got everything working. Thanks so much for all the help! :D

    I'm sure I'll be back with more questions throughout the semester. :o

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 02:11 PM
  2. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 12:06 AM
  3. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM
  4. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •