Solved

Scanner class for String

Posted on 2006-11-19
3
207 Views
Last Modified: 2010-03-31
String user_input[] = sc.next();

received an error "incompatible types - found java.lang.String but expected java.lang.String[]"

any idea how to fix this?
0
Comment
Question by:ishcmc
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17974125
next() returns String http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#next()


You can use findInLine method, combined with regex:

     String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input);
     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
     MatchResult result = s.match();
     for (int i=1; i<=result.groupCount(); i++)
         System.out.println(result.group(i);
     s.close();
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17974135
If you want to assign to an array, you'll have to construct one big enough first:

String user_input[] = new String[64];
int inputIndex = 0;
user_input[inputIndex++] = sc.next();




0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17981680
:-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

13 Experts available now in Live!

Get 1:1 Help Now