Fast Simple Question

very simple if else for a flowchart

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1770 Views - Last Post: 12 January 2010 - 12:47 AM Rate Topic: -----

#1 rms7936  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 187
  • Joined: 22-October 09

Fast Simple Question

Post icon  Posted 11 January 2010 - 07:26 PM

I am making a very simple code for a flowchart.
I am encountering a brain block though.
import java.util.Scanner;

public class exam {

	
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		boolean answer1;
		String answer2;
		String answer3;
		
		System.out.println ("Are you Christian?");
		answer1 = reader.next();
		if (answer1);
	}

}



I am running into errors with the "answer1 = reader.next();"
How do i make it so it reads for Boolean? It isn't as of now.
I know this is very simple and i appreciate any of you who take the time to help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Fast Simple Question

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: Fast Simple Question

Posted 11 January 2010 - 07:28 PM

You can use the Scanner nextBoolean() method to do so.
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Fast Simple Question

Posted 11 January 2010 - 07:29 PM

reader.nextBoolean() should do the trick. Reading API's do wonders...
Here's a link: http://java.sun.com/...il/Scanner.html

EDIT: wow THREE people?!

This post has been edited by Dogstopper: 11 January 2010 - 07:30 PM

Was This Post Helpful? 1
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Fast Simple Question

Posted 11 January 2010 - 07:29 PM

Scanner has a nextBoolean() method which would probably be what you're looking for.

http://java.sun.com/...l#nextBoolean()

edit: ahh. you both beat me to it ;)

This post has been edited by erik.price: 11 January 2010 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 rms7936  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 187
  • Joined: 22-October 09

Re: Fast Simple Question

Posted 11 January 2010 - 07:34 PM

Thanks all. Sorry for the simple question. Thanks for the links.
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Fast Simple Question

Posted 11 January 2010 - 07:35 PM

No problem. :)

By the way, your signature made me laugh :^:
Was This Post Helpful? 0
  • +
  • -

#7 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 138
  • Joined: 10-January 10

Re: Fast Simple Question

Posted 11 January 2010 - 07:49 PM

reader.nextBoolean();

and

reader.nextLine();

will both work. nextLine will require a more involved if statement though, just a heads-up.
Was This Post Helpful? 0
  • +
  • -

#8 rms7936  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 187
  • Joined: 22-October 09

Re: Fast Simple Question

Posted 11 January 2010 - 09:36 PM

I can't get the if statement to work now...
I seriously can't think tonight.
What am I supposed to do with it?
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Fast Simple Question

Posted 11 January 2010 - 09:39 PM

When comparing strings, remember to always use the .equals method when comparing strings!

if (answer.equals("The Answer))



EDIT: DOH...what did I smoke!

This post has been edited by Dogstopper: 11 January 2010 - 09:49 PM

Was This Post Helpful? 1
  • +
  • -

#10 rms7936  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 187
  • Joined: 22-October 09

Re: Fast Simple Question

Posted 11 January 2010 - 09:44 PM

View PostDogstopper, on 11 Jan, 2010 - 08:39 PM, said:

When comparing strings, remember to always use the .equals method when comparing strings!

if (answer.equals("The Answer))


"Cannot invoke equals(String) on primitve type boolean"
i get this error
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Fast Simple Question

Posted 11 January 2010 - 09:48 PM

Absolutely right! I was SO WRONG ON THAT. Sorry. When entering the answer, you must type "true" or "false", and in the if statement, do this:

if (answer)
	System.out.println("Something");
else
	System.out.println("Other stuff);


This post has been edited by Dogstopper: 11 January 2010 - 10:02 PM

Was This Post Helpful? 0
  • +
  • -

#12 rms7936  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 187
  • Joined: 22-October 09

Re: Fast Simple Question

Posted 11 January 2010 - 09:56 PM

View PostDogstopper, on 11 Jan, 2010 - 08:48 PM, said:

Absolutely right! I was SO WRONG ON THAT. Sorry. When entering the answer, you must type "True" or "False", and in the if statement, do this:

if (answer)
	System.out.println("Something");
else
	System.out.println("Other stuff);


Thank you again!
I think I've got it all figured out now.
-sigh-
All this effort for some measly extra credit.

import java.util.Scanner;

public class exam {

	
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		boolean answer1;
		String answer2;
		String answer3;
		
		System.out.println ("Are you Christian?");
		answer1 = reader.nextBoolean();
		boolean yes = false;
		if (answer1 == yes)
			System.out.println("Fail");
		else
			System.out.println("next question");
	}

}




I get this message:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextBoolean(Unknown Source)
at exam.main(exam.java:13)
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Fast Simple Question

Posted 11 January 2010 - 10:02 PM

Look at me! made ANother typo! It has been fixed. Make sure when you turn it in to type it exactly like that. Now it works for me!
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Fast Simple Question

Posted 11 January 2010 - 10:06 PM

Nver tried nextBoolean() on a Scanner
what is a valid boolean input: "T", "F", 'true", "false", "True", "False", "TRUE", "FALSE" ?
Better to read a String (scanner.next()) if check if the first character of the String is "Y" or "N" ignoring the case
Was This Post Helpful? 1
  • +
  • -

#15 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 138
  • Joined: 10-January 10

Re: Fast Simple Question

Posted 11 January 2010 - 10:49 PM

import java.util.Scanner;

public class exam {

   
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		boolean answer1;
		String answer2;
		String answer3;
	   
		System.out.println ("Are you Christian?(y/n): ");
		answer1 = reader.nextLine();
		if (answer1.equalsIgnoreCase("y"))
		   //insert what it needs to do if true
		else
		  //insert what to do if false
	}

}


My head hurts too much from working on my stuff to really think of a way to explain it without just typing the code so here's a freebie.

- Zach
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2