Solved

Scanner class for String

Posted on 2006-11-19
3
210 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

861 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