7 Replies - 1431 Views - Last Post: 05 November 2012 - 08:55 PM Rate Topic: -----

#1 raydio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 07:43 PM

Hello!

I am learning to program and am working on a simple chat program at the moment.
The issue is that I keep getting the wrong output for my input at the second tier of questions. It seems as though it is disregarding my "input2" and just using "input1" to determine which conditional is executed some reason.

Thanks in advance for your help!

//11.5.12

import java.util.*;

public class Chatbot
{
	public static void main(String[] args)
	{
		Boolean loop = true;

		while(loop)
		{
			Scanner scanner1 = new Scanner(System.in);
			System.out.println("Do you believe in God?");
			String input1 = scanner1.nextLine();
			loop = false;

			//Yes
			if(input1.equalsIgnoreCase("yes") || input1.equalsIgnoreCase("y"))
			{
				Scanner scanner2 = new Scanner(System.in);
				System.out.println("Are you living a good life in the eyes of your God?");
				String input2 = scanner1.nextLine();

				if(input2.equalsIgnoreCase("yes") || input2.equalsIgnoreCase("y"))
				{
					System.out.println("Good for you! You can now die happy :)/>");
				}
				else if(input2.equalsIgnoreCase("no") || input2.equalsIgnoreCase("n"))
				{
					System.out.println("That is unfortunate, might I sugest converting to the First Church of Carl Sagan?");
				}
				else
				{
					System.out.println("Don't avaoid the question! Yes or No?");
					loop = true;
				}
			}
			//No
			else if(input1.equalsIgnoreCase("no") || input1.equalsIgnoreCase("n"))
			{
				Scanner scanner2 = new Scanner(System.in);
				System.out.println("Are you living a fulfilling and meaningfull life?");
				String input2 = scanner2.nextLine();

				if(input2.equalsIgnoreCase("yes") || input2.equalsIgnoreCase("y"))
				{
					System.out.println("Great! You can now die happy :)/>");
				}
				else if(input2.equalsIgnoreCase("no") || input2.equalsIgnoreCase("n"))
				{
					System.out.println("It is time you create meaning in your life. Find something to care about!");
				}
				else
				{
					System.out.println("Don't avaoid the question! Yes or No?");
					loop = true;
				}	
					
			}
			//Maybe
			else
			{
				System.out.println("Don't avoid the question! Yes or No?");
				loop = true;
			}
		}

	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Runtime error with conditional if statements (chatbot program)

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 07:55 PM

What's your input and what's the output? It seems to be working for me.
Was This Post Helpful? 0
  • +
  • -

#3 raydio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 08:18 PM

View PostfromTheSprawl, on 05 November 2012 - 07:55 PM, said:

What's your input and what's the output? It seems to be working for me.


"Yes" and "No"

from the command prompt:

*********$ java Chatbot
>Do you believe in God?
>yes
>Are you living a good life in the eyes of your God?
>no
>Good for you! You can now die happy :)

But the last line should be "That is unfortunate, might I sugest converting to the First Church of Carl Sagan?"
Was This Post Helpful? 0
  • +
  • -

#4 raydio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 08:24 PM

*"Yes" and "No" are the user inputs. Anything else should loop you back to the beginning.
Was This Post Helpful? 0
  • +
  • -

#5 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 08:42 PM

First of all you will only need 1 scanner. Any more and you are just waisting memory. As for the inputs, try printing them out. Maybe they aren't the exact inputs you are looking for.
Was This Post Helpful? 1
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 08:43 PM

But it's working for me:

Quote

Do you believe in God?
Yes
Are you living a good life in the eyes of your God?
No
That is unfortunate, might I sugest converting to the First Church of Carl Sagan?


Try recompiling your class through javac. Delete the class file first.
Was This Post Helpful? 1
  • +
  • -

#7 raydio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 08:51 PM

Thank you!! The scanner issue was one I was actually confused about, so I'm glad to hear that. And recompiling worked after deleting the class file. Many thanks! I guess I should call this a user error instead of runtime...
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Runtime error with conditional if statements (chatbot program)

Posted 05 November 2012 - 08:55 PM

Good to hear it's working now. ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1