Results 1 to 6 of 6
  1. #1
    claudius753 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Scanner class with a String

    Hi all,

    I'm new to the forum and am taking a Java class in college. I'm having a problem with one of my projects for class.

    My main class is working fine, but in my test program, I can't scan a string using the Scanner. It doesn't even wait for input. Everything looks right to me, could it have something to do with Java on the Mac?

    Here is my code snippet:

    Java Code:
    System.out.print("Enter the Color: ");
    color = input.nextLine();
    Any ideas? Yes, I declared a Scanner and yes, I declared the string color.

    Thanks.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    could you post the whole source code?
    freedom exists in the world of ideas

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    That looks fine, post the complete code, please.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    claudius753 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    
    public class TestTriangle {
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		double side1, side2, side3;
    		boolean filled;
    		String color;
    		
    		System.out.print("Enter Length of side 1 = ");
    		side1 = input.nextDouble();
    		
    		System.out.print("Enter Length of side 2 = ");
    		side2 = input.nextDouble();
    		
    		System.out.print("Enter Length of side 3 = ");
    		side3 = input.nextDouble();
    		
    		System.out.println("Enter the Color: ");
    		color = input.nextLine();
    		
    		System.out.print("Is it filled?  true or false: ");
    		filled = input.nextBoolean();
    		
    		/* Make 3 Triangles... */
    		
    		/* Default constructor Triangle: */
    		Triangle defaultTriangle = new Triangle();
    		
    		/* Triangle defined in the book exercise: */
    		Triangle bookTriangle = new Triangle(1.0, 1.5, 1.0);
    		bookTriangle.setColor("yellow");
    		bookTriangle.setFilled(true);
    		
    		/* User defined Triangle: */
    		Triangle userTriangle = new Triangle(side1, side2, side3);
    		userTriangle.setColor(color);
    		userTriangle.setFilled(filled);
    		
    		/* Print Default Triangle */
    		System.out.println("\nThe default Triangle has the following attributes");
    		System.out.println(defaultTriangle.toString());
    		System.out.println("Area: " + defaultTriangle.getArea());
    		System.out.println("Perimeter: " + defaultTriangle.getPerimeter());
    		System.out.println("Color: " + defaultTriangle.getColor());
    		System.out.print("Filled: ");
    		System.out.println((defaultTriangle.isFilled()) ? "Yes" : "No");
    		
    		/* Print Book Triangle */
    		System.out.println("\nThe Book's Triangle has the following attributes");
    		System.out.println(bookTriangle.toString());
    		System.out.println("Area: " + bookTriangle.getArea());
    		System.out.println("Perimeter: " + bookTriangle.getPerimeter());
    		System.out.println("Color: " + bookTriangle.getColor());
    		System.out.print("Filled: ");
    		System.out.println((bookTriangle.isFilled()) ? "Yes" : "No");		
    		
    		/* Print User's Triangle */
    		System.out.println("\nThe User's Triangle has the following attributes");
    		System.out.println(userTriangle.toString());
    		System.out.println("Area: " + userTriangle.getArea());
    		System.out.println("Perimeter: " + userTriangle.getPerimeter());
    		System.out.println("Color: " + userTriangle.getColor());
    		System.out.print("Filled: ");
    		System.out.println((userTriangle.isFilled()) ? "Yes" : "No");		
    
    	}
    }

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    That is because the "enter key" after you fill the side3 is being captured by the input.nextLine,
    see documentation on the method nextLine in Scanner

    try to replace nextLine to next : input.next(); ( assume to enter a word ) and observe
    freedom exists in the world of ideas

  6. #6
    claudius753 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:13 PM
  2. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 05:11 AM
  3. Can i use Java Scanner with String?
    By guitar-maniac in forum New To Java
    Replies: 5
    Last Post: 01-18-2010, 07:36 PM
  4. String tokenizing with Scanner
    By vijaygk in forum Advanced Java
    Replies: 2
    Last Post: 07-15-2008, 04:44 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

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