Solved

Scanner class for String

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

867 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

15 Experts available now in Live!

Get 1:1 Help Now