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);
    }
}
}

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.

Thank you. buddy! :)

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

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.

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.

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.

Thank you guys!

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.