Solved

Scanner for string

Posted on 2006-11-18
14
166 Views
Last Modified: 2010-03-31
for int

var = sc.nextInt();

how do I do it for a string?
0
Comment
Question by:ishcmc
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17972373
whats the class sc?
0
 

Author Comment

by:ishcmc
ID: 17972397
scanner
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17972495
is this java.lang class? which library you are using?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17972518
if you are using java.util.Scanner, you can call sc.next() to get the next string token
0
 

Author Comment

by:ishcmc
ID: 17973671
Can't, doesn't work.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17973711
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
 
LVL 13

Expert Comment

by:marchent
ID: 17973829
String var = sc.nextLine();
0
 

Author Comment

by:ishcmc
ID: 17974033
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
 

Author Comment

by:ishcmc
ID: 17974037
The program then compiled fine, but when I ran it, it print out this instead of the question

[Ljava.lang.String;@ba5c7a
0
 
LVL 13

Expert Comment

by:marchent
ID: 17974055
use this

String questions = "What is the capital of Great Britain? ";
String answers = "London";
0
 

Author Comment

by:ishcmc
ID: 17974061
@marchent
but I will add more questions in later on, I have to use String
0
 
LVL 13

Expert Comment

by:marchent
ID: 17974068
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
 

Author Comment

by:ishcmc
ID: 17974081
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
 
LVL 13

Accepted Solution

by:
marchent earned 500 total points
ID: 17974087
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question