The first block is all of my methods and stuff and the second block is just the driver. i'm trying to get it to ask the user if it wants to repeat the whole thing again, but it only works with the last 2 choices

import java.util.*;

public class methodsForTest
{
	Scanner input = new Scanner(System.in);

	public void choose()
	{
		System.out.println("Which method do you want to do?");
		System.out.println("Choose from:");
		System.out.println("\t square \n\t rectangle \n\t triangle\n");
		String choice = input.nextLine();
		
		if(choice.equals("square"))
			{
				square();
			}
		
		else if(choice.equals("rectangle"))
		{
			rectangle();
		}
			
		else if(choice.equals("triangle"))
		{
			triangle();
		}		
		
		else
		{
			System.out.println("That's not a choice.");
		}
	}
	
	public void square()
	{
		System.out.println("Enter length of side.");
		int side = input.nextInt();
		
		for (int x = 0; x<side; x++)
			{
				for (int y = 0; y<side; y++)
				{
					System.out.print("*");
				}
				System.out.println();
			}
		System.out.println();
	}
	
	public void rectangle()
	{
		System.out.println("Enter height.");
		int height = input.nextInt();
		System.out.println("Enter width.");
		int width = input.nextInt();
		for (int x = 0; x<height; x++)
			{
				for (int y = 0; y<width; y++)
				{
					System.out.print("*");
				}
				System.out.println();
			}
		System.out.println();
	}
	
	public void triangle()
	{
		System.out.println("Enter  side length. This method will print out an equilateral triangle.");		
	}
	
	public String repeat()
	{
		System.out.println("Another method? \nChoose yes or no.");
		String repeat = input.nextLine();
		return repeat;
	}
}
import java.util.*;


public class test
{
	Scanner input = new Scanner(System.in);
	
	public static void main(String[] args)
	{
		String repeat = "yes";
		 while (repeat.equals("yes"))
		{
			methodsForTest choice = new methodsForTest();
		
			 choice.choose();
			
			repeat = choice.repeat();
		}	
	}
}

Recommended Answers

All 5 Replies

You need to change your code a little bit. The Scanner class is very inconsistent when it comes to handling tokens. If you read a line using nextLine() and then read a primitive type such as an integer/double/float/whatever the omitted newline character from the previous call will cause trouble.

So here's two different solutions to your problem.

1. Put a input.nextLine() call directly after your calls to read primitives
2. Use nextLine() exclusively, i.e when you want to read an int you do it as

int width = Integer.parseInt(input.nextLine());

Hope that helps!

sorry, but i forgot to mention that when the user chooses square or rectangle and then the dimensions, the output shows the shape of asterisks, then asks if they want to do it again but it doesn't let them choose. it treats them like they answered no and just exits the program.

you can do what Slimmy has suggested and see if that works, unless you want to go with the idea of doing a do{ }while(boolean = true). Let me know what other problems you have.

you can do what Slimmy has suggested and see if that works, unless you want to go with the idea of doing a do{ }while(boolean = true). Let me know what other problems you have.

Just curious here, how would using a do-while solve his problem with Scanner handling tokens in a bad way?

then asks if they want to do it again but it doesn't let them choose. it treats them like they answered no and just exits the program.

What I meant was, Do what Slimmy said for the Scanner but I said it weird. As for your loop problem try a do while loop to make it easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.