Solved

Scanner for string

Posted on 2006-11-18
14
164 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
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 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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

823 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