Results 1 to 3 of 3

Thread: Splitting

  1. #1
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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");
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    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

  1. Need Help splitting up an Int in a variety of ways
    By JoKeR313 in forum New To Java
    Replies: 9
    Last Post: 11-10-2010, 06:22 AM
  2. String Splitting
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 07:17 AM
  3. Reading files and splitting them
    By Dieter in forum Advanced Java
    Replies: 5
    Last Post: 09-30-2009, 09:46 AM
  4. Array splitting
    By Lunarion in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 08:00 AM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •