Results 1 to 9 of 9
  1. #1
    new_cyber is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default [SOLVED]Java split string, the first char is a space

    Hi guys,
    I'm doing 2 splits from a txt file, the 1st split saves the 1st char of the file
    the next split saves the rest of the line.
    For example the line is:
    "Ab Just an example"
    The first split saves.me the word "Ab" but the other split saves the phrase:
    " Just an example" the thing is that I get an empty space in the begining of the phrase, what I need to change?

    The code that I'm using is:
    firstChar=txtFile.next().split(" ");
    theRestPhrase=txtFile.nextLine().split("\n");
    Thanks and sorry my poor english.
    Regards
    Last edited by new_cyber; 12-30-2009 at 03:26 PM. Reason: Question solved

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I think you mean you are trying to get the first word and then the rest of the line.

    split() actually returns an array of strings so it's a bit hard to understand the code you posted. Perhaps you could post a small compilable example that is giving the undesirable output?

  3. #3
    new_cyber is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Yes, that What I want, to get the firsd word and then the rest of the line.
    I'm using a file reader, the thing is , I'm reading a .txt file, the first split
    firstChar=txtFile.next().split(" ");
    saves the frist word and I do other split to save the rest of the line
    theRestPhrase=txtFile.nextLine().split("\n");
    and it works, but the begining of the rest of the line is an empty space and I want to remove the empty space but I dont know how.

  4. #4
    gcampton Guest

    Default

    ok do this:

    Java Code:
    String myString = "Ab Just an example";
    String firstWord, restOfString="";
    
    String[] aSplit = myString.split(" ");
    
    firstWord=aSplit[0];
    
    // now for the rest of string we can just append the rest
    for (int i=1; i<aSplit.length; i++)
    {
        restOfString += aSplit[i];
        restOfString += " ";
    }
    
    System.out.println("First word = [" + firstWord + "]");
    System.out.println("Rest of string = [" + restOfString + "]");
    output
    Java Code:
    First word = [Ab]
    Rest of string = [Just an Example]
    Last edited by gcampton; 12-30-2009 at 03:24 PM.

  5. #5
    new_cyber is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by gcampton View Post
    ok do this:

    Java Code:
    String myString = "Ab Just an example";
    String firstWord, restOfString="";
    
    String[] aSplit = myString.split(" ");
    
    firstWord=aSplit[0];
    
    // now for the rest of string we can just append the rest
    for (int i=1; i<aSplit.length; i++)
    {
        restOfString += aSplit[i];
        restOfString += " ";
    }
    
    System.out.println("First word = [" + firstWord + "]");
    System.out.println("Rest of string = [" + restOfString + "]");
    output
    Java Code:
    First word = [Ab]
    Rest of string = [Just an Example]
    Thanks mate, it worked.

  6. #6
    gcampton Guest

    Default

    Quote Originally Posted by new_cyber View Post
    Thanks mate, it worked.
    Do you understand how to use .split() from my example now?

    it's a lot easier to make arrays and use arrays on the string.split() then trying to use scanner and split combined.

    if you want to use scanner then use the scanner.Delimiter(" "); method, it will do your way of trying to break it up. (.next() & .nextLine())

    Just look up the Scanner API

  7. #7
    new_cyber is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by gcampton View Post
    Do you understand how to use .split() from my example now?

    it's a lot easier to make arrays and use arrays on the string.split() then trying to use scanner and split combined.

    if you want to use scanner then use the scanner.Delimiter(" "); method, it will do your way of trying to break it up. (.next() & .nextLine())

    Just look up the Scanner API
    Yes, looking the example you gave me, I modified my code and it works now , really apreciate your help.

    Have a happy new year.
    Regards

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A more accurate approach would be to avoid arrays and use String methods: indexOf() will find the first space and the two variants of substring() will enable the word and the rest of the line to be extracted. trim() might also be useful when dealing with the second part.

  9. #9
    gcampton Guest

    Default

    And another way of doing it.
    Java Code:
    import java.util.Scanner;
    public class stringTest 
    {
        public static void main(String[] args) 
        {
            String abc = "Abc Rest of String goes here";
            String firstWord, restOfString;
            Scanner sc = new Scanner(abc);
            
            firstWord = sc.next();
            restOfString = sc.nextLine();
            
            System.out.println(firstWord);
            System.out.println(restOfString);
            
            restOfString = restOfString.substring(1,restOfString.length());
            //    restOfString = restOfString.trim();   = less typing :D
            System.out.println(restOfString);
        }
    }
    Last edited by gcampton; 12-31-2009 at 01:31 AM.

Similar Threads

  1. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 02:23 PM
  2. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  3. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 02:09 AM
  4. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM
  5. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 01:42 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
  •