Solved

Scanner for string

Posted on 2006-11-18
14
168 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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