1,105,225 Community Members

Please help a newbie debug a simple Triangle-asterisks program

Member Avatar
larrymtl
Newbie Poster
18 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

somehow my program passes directly to else statement after

System.out.println("Would you like to draw another triangle? (Yes/No): ");

something is wrong with if statement at the end...please help

import java.util.*;

public class Triangle 

{

    public static void main (String [] args)
    	
	{
	
	System.out.println("Welcome to the Triangle Program");
	 
	int size;
	String rightAnswer = "Yes";
	
	System.out.println("How big of a triangle would you like: ");
	Scanner input = new Scanner(System.in);
	size = input.nextInt();
	
	if
		(size <= 0)
		{
			System.out.println("Please input a positive integer number for the size of the triangle.");
			System.out.println("How big of a triangle would you like: ");
			size = input.nextInt();
 		}
 		
	System.out.println("Here is your right-angle triangle of size " + size);
 
	for (int i=0; i<size; i++)
	{
		for (int f=0; f<i; f++)
		{
		System.out.print("*");
		}
	System.out.println("*");
 
	}
	
	System.out.println("Would you like to draw another triangle? (Yes/No): ");
	
	String answer =  input.nextLine();
	
	
	if 
		(rightAnswer.equalsIgnoreCase(answer))
	
	{
		
	System.out.println("How big of a triangle would you like: ");
	
	size = input.nextInt();
	
	if
		(size <= 0)
		{
			System.out.println("Please input a positive integer number for the size of the triangle.");
			System.out.println("How big of a triangle would you like: ");
			size = input.nextInt();
 		}
 
	System.out.println("Here is your right-angle triangle of size " + size);
 
	for (int i=0; i<size; i++)
	{
		for (int f=0; f<i; f++)
		{
		System.out.print("*");
		}
	System.out.println("*");
 
	}
    
    }
    
    else 
    {
    
    	System.out.println("Thank you for using the Triangle Program");
    	System.exit(0);
    }
}
}
Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

first remark: why do you put in two times the logic to print that asterisk figure?
you could do something like (just the logic, not in Java syntax :P)

boolean repeat = true;

while repeat{
drawFigure();
print( "do you wish to draw again(Yes/No)")
repeat = answer;
}

but, to go back to your problem: I've tried to run your code here, and didn't even get to answer the Yes/No question, since it automatically jumped to the next line, and answer was filled with a default value, which means it would never be equal to "yes".

When I checked your code, I saw the problem: you've tried entering your answer like this:

String answer =  input.nextLine();

which causes the exact problem I just described.
a look in the api for the Scanner class quickly provided a sollution for this. Remember, to read either Yes or No, you don't need to read an entire line, you just need to read the next token.
so, just replace the line above, with:

String answer =  input.next();

you can find the api's I told you about right here if you need some more explanation about this.

Member Avatar
larrymtl
Newbie Poster
18 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you. buddy! :)

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

if it works, just mark this thread as solved, so n one 'll get in here looking to provide a sollution :)

Member Avatar
larrymtl
Newbie Poster
18 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Well, this works partially, for "yes" its recognizes, for "y" - not.

I tried different solutions with

if (rightAnswer.equalsIgnoreCase(answer))

but I gave up. There is something simple I don't know.

Member Avatar
arka.sharma
Newbie Poster
10 posts since Jan 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

One thing that I think important is that you have written the entire code inside main.You should take a separate class and method for doing this.
Another point is that when a user enters negative size you are doing the checking for once but the user can enter negative size again.You haven't handle that.However these things not related to your problem but you should keep these things in mind while programming.

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

Well, this works partially, for "yes" its recognizes, for "y" - not.

I tried different solutions with

but I gave up. There is something simple I don't know.

which basically means ... it works.

just take a look at your original code:

....
String rightAnswer = "Yes";
 
....
System.out.println("Would you like to draw another triangle? (Yes/No): ");
 
String answer = input.nextLine();
  
if
(rightAnswer.equalsIgnoreCase(answer))
 ...

you are checking here for equality of value (not case sensitive). you're not checking whether or not answer is a substring of rightanswer.

so ... now you'll need to be clear about when you want the if to run. on each substring from rightAnswer (y, ye, e, es, yes, s) or only on yes and y.

my suggestion for the first:
use the indexOf() method to see if the substring is in rightAnswer or not. off course, there is no 'indexOfIgnoreCase()' method, but if you want it to stay case insensitive, you can do the next:
set both rightAnswer and answer to lowercases, like this:

rightAnswer = rightAnswer.toLowerCase();

before checking to see if the substring is present.

in case you want the second scenario: just 'Y' or 'y'

well ... just check whether or not 'answer' has a length of 1. if this is the case, you might have entered 'y' or 'Y'.
so, again: if you wish to keep the case insesitivity, set both the String objects to lower cases, like I showed above. now check the equality of answer.charAt(0) and rightAnswer.charAt(0).

This should help you out on that point.

@arka.sharma
1. if you look very closely to the OP's objective, you see that it is a very basic problem, which is often given to people just starting to learn to work with Java. as the wise ones say, crawl, before you run. at this point, I assume it is more important for him to learn the basics on how to use conditions and such, than to master the entire world of Object Oriented programming. now, his teacher, or the author of the book/tutorial he's following obviously figured this out, and propably instructed him to write this in the main.
2. whether the user inputs a negative number or not, is not the issue here. yes, it would be easily solved by adding a simple if-statement, but just as point 1, his assignment may as well have said, write it as if the user can not enter negative numbers, and don't wory with handling this event if it occurs. so, don't look for problems that aren't there, it might confuse him even more, which is, I hope, not your intention.

Member Avatar
larrymtl
Newbie Poster
18 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you guys!

Question Answered as of 2 Years Ago by stultuske and arka.sharma
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: