?
Solved

Scanner for string

Posted on 2006-11-18
14
Medium Priority
?
169 Views
Last Modified: 2010-03-31
for int

var = sc.nextInt();

how do I do it for a string?
0
Comment
Question by:ishcmc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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