Solved

Scanner class for String

Posted on 2006-11-19
3
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
how to see all occupied ports on windows 10 laptop 15 85
maven disable workspace resolution 1 43
Setup GlassFish 4 37
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

730 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