Need some help with java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • inevitabledarkness
    Banned
    • Dec 2006
    • 82

    #1

    Need some help with java

    Alright, I'm doing the program that asks for the length and height of a box. That part I got. What I can't seem to get is the part where it asks for an input by the user of which character to use when creating the box. Here's the code. Please help.
    EDIT: Cleaner code.
    Code:
    import java.util.Scanner;
    import java.lang.*;
    
    /**
     * Michael Cuiffi
     * Period 8
     * 2/1/07
     **/
     
     public class DrawBox {
     	
     	public static void drawBox(int length, int height, String mark) {
     	
     		for (int i = 1; i <= height; i++){
     			drawBar(length, mark);
     		}
     		System.out.println();
     	}	
     	public static void drawBar(int length, String mark) {
     		
     		for (int i = 1; i <= length; i++) {
     			System.out.print(mark);
     		}
     		System.out.println();
     	}	
     		public static void main(String[] args) {
     			
     			int length, height;
     			String mark;
     			String choice;
     			Scanner s = new Scanner(System.in);
     			
     			System.out.print("Please enter the box's length: ");
     			length = s.nextInt();
     			System.out.print("Please enter the box's height: ");
     			height = s.nextInt();
     			System.out.print("Do you want to enter a character to use to display the box?\n(enter y for yes): ");
     			choice = s.nextLine();
     			System.out.println();
     			
     			if (choice != "Y") {
     				drawBox(length, height, "*");
     			} else if (choice == "Y") {
     				System.out.print("Please enter the character: ");
     				mark = s.nextLine();
     				drawBox(length, height, mark);
     			}
     			
     			
     		
     	}
     	
     	}
    Last edited by inevitabledarkness; 02-5-2007, 02:03 PM.
  • AirForceBlue
    FFR Player
    • Mar 2007
    • 64

    #2
    Re: Need some help with java

    Hrm, what you want to use is an InputBox, most probably. I'm not really familiar with Java, but I'll do some research and edit this post when I find something out.

    Comment

    • RandomPscho
      FFR Player
      • Jun 2006
      • 504

      #3
      Re: Need some help with java

      InputBox NO WAY!!! -- I hate that book AirForce CSLib UGH!
      EDIT: Woops, misread.
      EDIT:

      Ok, the problem:
      Code:
      System.out.print("Do you want to enter a character to use to display the box?\n(enter y for yes): ");
       			[b]choice = s.nextLine();[/b]
      Should be
      choice = s.next();
      I am not sure what nextLine() actually does, but next() reads the user's string.

      Also: you might want to add if (choice != "Y" || choice !="y") to account for lowercase y's
      Last edited by RandomPscho; 03-11-2007, 03:23 PM.

      Comment

      • MRTL_mrclean17
        FFR Player
        • Nov 2006
        • 156

        #4
        Re: Need some help with java

        I was testing this script and was wondering why nothing was showing up, then it dawned on me, this is Java, not JavaScript, which the former happens to be my specialty. Sorry I'm of no help.

        Comment

        • Zovistograt
          FFR Player
          • Apr 2007
          • 27

          #5
          Re: Need some help with java

          ARGH, no, please, never use boolean with Strings. Use

          Code:
          choice.equalsIgnoreCase(String)
          instead. Also, use an else if thing like this...

          Code:
          if (choice.equalsIgnoreCase("Y")) System.out.println("some code...");
          else if (choice.equalsIgnoreCase("N")) System.out.println("other code...");
          else System.out.println("enter Y or N only!");
          and 'course you should put that in a little while loop so that if they don't enter Y or N it just says it again.
          Last edited by Zovistograt; 04-15-2007, 05:42 PM. Reason: forgot some end parentheses D:
          http://spzc.net/

          Comment

          • Zovistograt
            FFR Player
            • Apr 2007
            • 27

            #6
            Re: Need some help with java

            Originally posted by RandomPscho
            InputBox NO WAY!!! -- I hate that book AirForce CSLib UGH!
            EDIT: Woops, misread.
            EDIT:

            Ok, the problem:
            Code:
            System.out.print("Do you want to enter a character to use to display the box?\n(enter y for yes): ");
             			[b]choice = s.nextLine();[/b]
            Should be

            I am not sure what nextLine() actually does, but next() reads the user's string.

            Also: you might want to add if (choice != "Y" || choice !="y") to account for lowercase y's
            nextLine() does the same thing...I thought you only use next() for Scanner stuffs with delimeters and all that semi-confusing annoyance.
            http://spzc.net/

            Comment

            Working...