Mark Yo
Mark Yo

Reputation: 49

java split method to arrays

I have a data like below:

3.3 John
3.9 Jim
3.5 Kathy
3.2 Steve
2.4 Stacy
2.8 Faith

Code:

File StudentGrade = new File("data.txt");
    Scanner reader = new Scanner(StudentGrade);
    int size = reader.nextInt();
    //debug: System.out.println(size);

    String[] gradeArr = new String[size];// array created for grades
    //debug: System.out.println(gradeArr);
    String[] nameArr = new String[size];//array created for name
    String[] temp = new String[100];// array created for temporary array
    int index = 0;//index indicator

    while (reader.hasNextLine()) {
        String s = reader.nextLine();
        //System.out.println(s);
        temp = s.split("\\s+");
        gradeArr[index] = temp[0];
        nameArr[index] = temp[1];
        System.out.println("grades= "+ gradeArr[index]+ " name = "+ nameArr);
        index++;
    }

It has to be split to 2 arrays, one is for the numbers, another is for names. I can get the numbers to temp[], but I don't know how to convert names to another array. Please teach me.

Upvotes: 0

Views: 403

Answers (3)

Vishnu
Vishnu

Reputation: 89

import java.io.*;
import java.util.*;

class HelloTest {
 public static void main(String args[]) {
  try {
   //data.txt contains 6 line data as in your question 
   File file = new File("data.txt");
   Scanner reader = new Scanner(file);
   int size = 6; // six line data so it is 6 
   String[] nameArr = new String[size]; //array created for name
   String[] gradeArr = new String[size]; // array created for temporary array
   int i = 0;
   while (size--> 0) {
    String s = reader.nextLine();
    String[] data = s.split(" ");
    nameArr[i] = data[1];
    gradeArr[i] = data[0];
    i++;
   }

   for (String name: nameArr) System.out.println("name is " + name);
   for (String grade: gradeArr) System.out.println("grade is " + grade);



  } catch (FileNotFoundException fe) {
   fe.printStackTrace();
  }
 }
}

Upvotes: 1

yassadi
yassadi

Reputation: 524

When you call split("\\s+"), the result array would look like this:

gradeArr[0] = 3.3;
gradeArr[1] = John;

So if you call gradeArr[1], you will get the name.

You need to call reader.nextLine(); after your reader.nextInt();. You need to read the enter character after the int so you would be on the next line of the .text file.

The following code works:

File StudentGrade = new File("E:/workspace/Test/src/main/java/data.txt");
        Scanner reader = new Scanner(StudentGrade);
        int size = reader.nextInt();
        // debug: System.out.println(size);
        reader.nextLine();
    String[] gradeArr = new String[size];// array created for grades
    // debug: System.out.println(gradeArr);
    String[] nameArr = new String[size];// array created for name
    String[] temp = new String[100];// array created for temporary array
    int index = 0;// index indicator

    while (reader.hasNextLine()) {
        String s = reader.nextLine();
        // System.out.println(s);
        temp = s.split("\\s+");
        gradeArr[index] = temp[0];
        nameArr[index] = temp[1];
        System.out.println("grades= " + gradeArr[index] + " name = " + nameArr[index]);
        index++;
    }

And here is the resutl:

grades= 3.3 name = John
grades= 3.9 name = Jim
grades= 3.5 name = Kathy
grades= 3.2 name = Steve
grades= 2.4 name = Stacy
grades= 2.8 name = Faith

data.text:

6
3.3 John
3.9 Jim
3.5 Kathy
3.2 Steve
2.4 Stacy
2.8 Faith

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201429

This looks like a good place to apply a regular expression, group the digit.digit white space and consequetive word characters. Iterate matches. Build two List(s). Like,

Pattern p = Pattern.compile("(\\d+\\.\\d+)\\s+(\\w+)");
List<Double> grades = new ArrayList<>();
List<String> names = new ArrayList<>();
String s = "3.3 John 3.9 Jim 3.5 Kathy 3.2 Steve 2.4 Stacy 2.8 Faith";
Matcher m = p.matcher(s);
while (m.find()) {
    grades.add(Double.valueOf(m.group(1)));
    names.add(m.group(2));
}
System.out.printf("%s %s%n", grades, names);

Outputs

[3.3, 3.9, 3.5, 3.2, 2.4, 2.8] [John, Jim, Kathy, Steve, Stacy, Faith]

Upvotes: 4

Related Questions