Results 1 to 3 of 3
Thread: Splitting
- 01-04-2011, 11:11 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Splitting
Very new to java
Simply, i want to split a user input wherever they have put .,! etc...
Then add each split to a separate array
my very basic code...
ArrayList array = new ArrayList();
System.out.println("Enter: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try{
input = br.readLine();
} catch (IOException e){
System.out.println("Error");
}
- 01-05-2011, 12:00 AM #2
Have a look here for a simple tokenizing method: StringTokenizer (Java Platform SE 6)
...Unless this is an assignment in which you are supposed to create the method yourself? I couldn't tell by your original post.
- 01-05-2011, 01:55 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
You can use the Scanner class to get input, and regex class to split on your input
Java Code:import java.util.Scanner; import java.util.regex.*; public class StringInput { public static void main(String[] args){ Scanner sc = new Scanner ( System.in ) ; System.out.println("Enter: "); String input = sc.nextLine(); sc.close(); Pattern p = Pattern.compile("[!.,]"); String[] tokens = p.split( input ); for( String str : tokens){ System.out.println("->"+str); } } }
Similar Threads
-
Need Help splitting up an Int in a variety of ways
By JoKeR313 in forum New To JavaReplies: 9Last Post: 11-10-2010, 06:22 AM -
String Splitting
By A.M.S in forum New To JavaReplies: 1Last Post: 12-04-2009, 07:17 AM -
Reading files and splitting them
By Dieter in forum Advanced JavaReplies: 5Last Post: 09-30-2009, 09:46 AM -
Array splitting
By Lunarion in forum New To JavaReplies: 3Last Post: 04-17-2009, 08:00 AM -
splitting string and replacing
By itsme in forum New To JavaReplies: 1Last Post: 12-11-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks