Results 1 to 4 of 4

Thread: Array splitting

  1. #1
    Lunarion is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default Array splitting

    I need help on figuring out how to split this array up to manipulate it. Basically I am importing from a text file with the following.

    "
    4
    141:She Loves You:Beatles
    171:Cliffs of Moher:Arcona Reel Band
    270:Long Black Veil:Jerry Garcia
    190:Ticket to Ride:Beatles
    "

    I need to set the numbers to integers to add and sort (high-low/low-high) and Basically the artists and songs in alphabetical order. Basically I just need to see how to split it up properly. Here's what I have so far


    Scanner input = new Scanner(new File("music.txt"));
    int x = input.nextInt(); //counts tracks
    input.nextLine();

    while (input.hasNextLine()) {
    String namesLine = input.nextLine();
    String line = input.nextLine();


    String[] names = namesLine.split(" ");



    x++;

    // int[] trackSum = findTotalLength(line);
    System.out.println(Arrays.toString(names)); //prints array

    }
    }


    please try not to give me the full answer. if you want to just give me something to pick at. thanks for any help.

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    String[] names = namesLine.split(" ");
    So your are splitting the line in separate words?

    The way you are doing it right now seems really backwards to me (using a scanner). I would recommend that you use a BufferedReader, wrapped around the file:
    Java Code:
    BufferedReader rd = new BufferedReader(new FileReader("music.txt"));
    then you can read from the file line by line:
    Java Code:
    String line;
    while((line = rd.readLine) !=null) {
            // process each line (line is an individual line in the file)
    }
    Then for each line i would recommend you use a regex to split it properly: Maybe split it based on the colon ':' character?
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

  3. #3
    Lunarion is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default

    I actually did the entire program with help of course. no need for anything. works 100%. I'm just a noob still with no background experience:)

  4. #4
    svpriyan is offline Member
    Join Date
    Mar 2009
    Location
    London
    Posts
    8
    Rep Power
    0

    Default

    hai
    what will happen if i have more than one space in this line and i am going to split them to 3 or 4 pieaces, Number of space= number of pieces
    for 1:
    String s2 = s.readLine();
    while (s2 != null) {
    String[] s3 = s2.split(" ");
    if (s3.length > 1) {
    myData.add(new FindNodes(s3[0], s3[1]));
    } else {
    myData.add(new FindNodes(s2));
    }
    s2 = s.readLine();
    }


Similar Threads

  1. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 03:25 AM
  2. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 09:59 PM
  3. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 06:57 PM
  4. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 04:08 PM
  5. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:32 AM

Posting Permissions

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