Scanner for string

for int

var = sc.nextInt();

how do I do it for a string?
ishcmcAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
marchentConnect With a Mentor Commented:
use user_input
if(user_input.compareToIgnoreCase(ans[0]) == 0)// i assume index is 0
//do code

u use user_input[] for multiple user answer
then
if(user_input[0].compareToIgnoreCase(ans[0]) == 0 )
//
0
 
Ajay-SinghCommented:
whats the class sc?
0
 
ishcmcAuthor Commented:
scanner
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Ajay-SinghCommented:
is this java.lang class? which library you are using?
0
 
Ajay-SinghCommented:
if you are using java.util.Scanner, you can call sc.next() to get the next string token
0
 
ishcmcAuthor Commented:
Can't, doesn't work.
0
 
suprapto45Commented:
Yep,

Ajay-Singh is right, you can use sc.next() to get the next string token.

http://www.java2s.com/Code/Java/Language-Basics/JavaformattedIOUseScannertocomputeanaverageofthevalues.htm

David
0
 
marchentCommented:
String var = sc.nextLine();
0
 
ishcmcAuthor Commented:
I am making a simple quiz.

String questions[] = {
                     "What is the capital of Great Britain? "};

String answers[] = {        "London"};

I want to print out the question first so I did

System.out.print(questions);

Then

String user_answer = sc.nextLine();

for user input. Compare user_answer to String answers and display appropriate message.

0
 
ishcmcAuthor Commented:
The program then compiled fine, but when I ran it, it print out this instead of the question

[Ljava.lang.String;@ba5c7a
0
 
marchentCommented:
use this

String questions = "What is the capital of Great Britain? ";
String answers = "London";
0
 
ishcmcAuthor Commented:
@marchent
but I will add more questions in later on, I have to use String
0
 
marchentCommented:
then use array
        String que[] ={"que0",
                      "que1",
                      "que2",
                      "que3"};
                     
        System.out.println(que[0]); //u have to show the index of que[], here is 0
        System.out.println(que[1]);
        System.out.println(que[2]);
        System.out.println(que[3]);

~marchent~
0
 
ishcmcAuthor Commented:
ok then I would have the same for answers

String ans[] ={"ans0",
                      "ans1",
                      "ans2",
                      "ans3"};

I also will need a variable for userinput

String user_input[];

How do I go on comparing user_input to ans?

if(user_input[] = ans[])
// instructions

That doesnt seem to work for me
0
All Courses

From novice to tech pro — start learning today.