import java.util.Scanner;

class apples {
	public static void main(String args[]){
		System.out.print("Hello, today I'm going to give you a quiz. \nWhat's your name?\n");
		Scanner name = new Scanner(System.in);
		System.out.print(name.nextLine());
		System.out.print(" Is a nice name.\nToday I am going to test your knowledge.\nLet's begin!\n");
		int score = 0;
		System.out.print("Who is Bill Gates?\n1. He made gates.\n2. He invented the computer and used to be the owner of Microsoft.\n3. He is the CEO and owns Apple.");
		Scanner one = new Scanner(System.in);
		if (one == 1){
			score--;
			System.out.print("WRONG! You lose 1 point\n");
		}
		if (one == 2){
			score++;
			System.out.print("CORRECT! You win 1 point");
		}
		if (one == 3){
			score--;
			System.out.print("WRONG! You lose 1 point\n");
		}
	}
}

Uh, Eclipse says it's on line 12 (if (one == 1){) but I don't know what's wrong.
Please help.:confused::?::icon_question:

it's on line 12 (if (one == 1){)

What is the "it's"? Do you get an error message?

Please copy and paste the full text of the error message here.

The equality test: one == 1
doesn't make sense. The variable 'one' can't be compared to an int using the == operator.
What are you trying to do? I think you need to read some data from the scanner object and then compare what you have read to 1.
Do a Search on the forum for examples of how to use the Scanner class.

Edited 6 Years Ago by NormR1: n/a

I'm trying to make it detect what one is and if it's 1 then it executes a block of code.

I dont think it is necessary to have 2 separate scanners. But anyway, what you are doing there seems to me like you are asking if a scanner variable called 'one' is equal to a number which doesn't make sense. I'm not 100% sure but maybe try:


Instead of having 2 scanner objects, just have one e.g.

Scanner kbd = new Scanner(System.in);
//then for your little question
int answer = kbd.nextInt();
//and then your if statments
if(answer == 1)
{
blabla
}....etc

I also think its better programming practice to create variables each time you want to take information from the user e.g

Scanner kbd = new Scanner(System.in);
String name = kbd.next();
System.out.print(name);

I am no expert, Ive only been studying java for a year. :D

Edited 6 Years Ago by Akill10: n/a

I'm trying to make it detect what one is and if it's 1

'one' is not an int variable and will never be equal to the int: 1.
'one' is a Scanner object. Objects are instances of classes that can do useful things for you in your program. You need to see what methods the Scanner class has that you can use to help you in your program.
Akill10 has shown you one method of the Scanner class that will read an int value into your program where you can test it.

But then how would I make it input the number?
Does the int answer do that job?

Do you mean how does the user input the number?

If so, then yes, that part of the code does that job. Everytime the next() method is called in any form(e.g. nextInt()..etc) The system waits for an input of some kind.

:)Oh, Cool! Thanks for the help everyone!
This forum is great!

Edited 6 Years Ago by TheSecOrg: n/a

I have read the comments above and your code:
You don't need to declare Scanner twice - once will do, like this:

Scanner in = new Scanner(System.in);

Next I recommend this for getting the user's name:

System.out.print("Hello, today I'm going to give you a quiz. \nWhat's your name?\n");
			String name = in.nextLine();

And then to get the user's choice you act in pretty much the same way:

System.out.println("Please press 1,2 or 3 accordingly: ");
			int choice = in.nextInt();

Finally when can use the if statement to check their answer by passing the "choice" variable to it, which you have done correctly. Anything else you unclear about because I got your program working fine.

No, nothing else I'm unclear about, thanks.
I used the switch statement instead, worked correctly.

No, nothing else I'm unclear about, thanks.
I used the switch statement instead, worked correctly.

Good to hear - why don't you post your code for others to reference and learn from then?
:P

Good to hear - why don't you post your code for others to reference and learn from then?
:P

Anything he was unclear with in this thread has been dealt with and explained. :) Therefore, adding the rest of his code could give somebody a complete copy/paste answer.

:cool:OK! :cool:

import java.util.Scanner;
public class quiz {
	public static void main (String args[]){
		System.out.print("Hello, today I am going to give you a quiz.\nWhat is your name?\n");
		Scanner name = new Scanner(System.in);
		System.out.print("Hello ");
		System.out.print(name.nextLine());
		System.out.print(", nice to meet you.\nGet ready for a quiz because it's starting!\n");
		System.out.print("Who is Bill Gates?\n1. He invented gates.\n2. He created the computer.\n3. He is the owner of Apple.\n");
		int one = name.nextInt();
		int score = 0;
		switch (one){
		case 1:
			System.out.print("WRONG! The correct answer is 2.");
			score--;
			break;
		case 2:
			System.out.print("CORRECT! You win 1 point!\n");
			score++;
			break;
		case 3:
			System.out.print("WRONG! The correct answer is 2.\n");
			score--;
			break;
			default:
				System.out.print("You're meant to input 1, 2 or 3.\nYou lose 1 point for inputting a wrong number.");
				score--;
				break;
		
		}
	}
}

It's not finished, I still need to add questions and do the results.

I would recommend renaming your scanner variable to something more related. Scanner object needs an input from a user, so use variable names like:
kbd - as in keyboard
in - as in input
sc - as in scanner

This makes it much easier to not get confused if your program begins to grow. You never know when 'name' could come in quite useful as another variable name.

I only use the Scanner for the name so no need to rename it.
Do I have to use hbd, in ect? I want to be able to 'Submit' the name variable with the score for that name to a website although this may be too hard for a newbie like me.

I only use the Scanner for the name so no need to rename it.
Do I have to use hbd, in ect? I want to be able to 'Submit' the name variable with the score for that name to a website although this may be too hard for a newbie like me.

You cannot do that. The scanner object does not hold information.

Scanner name = new Scanner(System.in);

On this line you are creating a Scanner object called 'name'. All the 'name' variable can now do is utilise the methods within the Scanner class.

So lets say the user input 2 different pieces of information using this method
e.g.

String firstName = name.next();
int one = name.nextInt();

The values of name.nextInt() and name.next() are not stored in the variable 'name'(i.e. the scanner object), they are stored in the variables one and firstName
If you are looking to hold information the user inputs into your program, e.g. Name, score etc., You need to start looking into creating classes and arrays.

And no, you don't have to use kbd,in,etc. you can call your variables whatever you like, but keep to naming them logically!

Btw, Bill Gates didn't invent the computer. He invented The operating system Microsoft Windows on which he copied the source code of Steve Jobs Operating System and then added to it his own implementations/ideas.

Thanks Akill10 and yasuodancez, everything (that I need atm) is clear now and I know the correct answer to the Bill Gates question.

This question has already been answered. Start a new discussion instead.