Welcome to Dream.In.Code
Become a Java Expert!

Join 150,385 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,159 people online right now. Registration is fast and FREE... Join Now!




using Scanner class

 
Reply to this topicStart new topic

using Scanner class

jojo515
19 Feb, 2008 - 01:33 AM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 9

I have a problem with this code. When i run it...the program just stops at the last drawBox. The cursor stays there and it doesn't prompt me to "press any key to continue..."
how do i fix this? i have more parts to my program but it won't run after the last method in this excerpt of my code is executed.

CODE

// draw four boxes
        drawBox();
        System.out.print("Enter height: ");
        int height1 = keyboard.nextInt();
        System.out.println();
        System.out.print("Enter width: ");
        int width1 = keyboard.nextInt();
        System.out.println();
        drawBox(height1,width1);
        System.out.print("Enter height: ");
        int height2 = keyboard.nextInt();
        System.out.println();
        System.out.print("Enter width: ");
        int width2 = keyboard.nextInt();
        drawBox(height2,width2, '+');
        System.out.print("Enter height: ");
        int height3 = keyboard.nextInt();
        System.out.println();
        System.out.print("Enter width: ");
        int width3 = keyboard.nextInt();
        System.out.println();
        drawBox(height3,width3, '#');


This post has been edited by jojo515: 19 Feb, 2008 - 01:37 AM
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Using Scanner Class
19 Feb, 2008 - 01:37 AM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Could we have the code of the drawBox functions. The problem is probably inside that, and we can't help you if we can't see it.

Thanks.
User is offlineProfile CardPM
+Quote Post

jojo515
RE: Using Scanner Class
19 Feb, 2008 - 01:43 AM
Post #3

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 9

here are the different drawBox methods codes...

CODE

    //
    // drawBox method version 1
    //
    public static void drawBox() {
        for (int line = 1; line <= 4; line++) {
            System.out.print("*");
        }
        System.out.println();

        for (int line = 1; line <= 4-2; line++) {
            System.out.print("*");
            for (int i = 1; i <= 2; i++) {
                System.out.print(" ");
            }
            System.out.println("*");
        }

        for (int line = 1; line <= 4; line++) {
            System.out.print("*");
        }
        System.out.println();
    }

    //
    // drawBox method version 2
    //
    public static void drawBox(int height, int width) {

        for (int line = 1; line <= width; line++) {
            System.out.print("*");
        }
        System.out.println();

        for (int i = 1; i <= height - 2; i++) {
            System.out.print("*");
            for (int j = 1; j <= width-2; j++) {
                System.out.print(" ");
            }
            System.out.println("*");

        }

        for (int line = 1; line <= width; line++) {
                    System.out.print("*");
        }
        System.out.println();
    }

    //
    //drawBox method version 3
    //
    public static void drawBox(int  height, int width, char symbol) {
        for (int line = 1; line <= width; line++) {
                System.out.print(symbol);
            }
            System.out.println();

            for (int line = 1; line <= height-2; line++) {
                System.out.print(symbol);
                for (int i = 1; i <= width-2; i++) {
                    System.out.print(" ");
                }
                System.out.println(symbol);
            }

            for (int line = 1; line <= width; line++) {
                System.out.print(symbol);
            }
            System.out.println();


another thing...
how do i have the user input a symbol instead of assigning it in my program? ...it's for the 3rd and 4th drawBox method calls
User is offlineProfile CardPM
+Quote Post

jojo515
RE: Using Scanner Class
19 Feb, 2008 - 01:49 AM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 9

ok nevermind... i figured out how to do it

Thanks.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Using Scanner Class
19 Feb, 2008 - 01:50 AM
Post #5

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
You didn't paste the entire code for the 3rd drawBox function. This is the one that I think is causing the problem.

To answer your second question, you can use a scanner like this:
CODE

char symbol;
Scanner scan = new Scanner(system.in);

symbol = scan.next().charAt(0);


The above code creates a scanner, gets a string, and gets the char at the start of the string. So if the user entered "#alsdfj" then symbol would become '#'.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month