Solved

Scanner for string

Posted on 2006-11-18
14
162 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now