Vaibhav Rai
Vaibhav Rai

Reputation: 183

Read from file and parse them into different arrays

I'll read a file content as an input for my program.

I'm storing it into a string and splitting it into 2 string.

File file = new File("question.txt");
PrintWriter pw = new PrintWriter(file);
pw.write("81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)");
pw.close();
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
Map<Integer, Object> map = new HashMap<Integer, Object>();
int i = 0;
Demo d = new Demo();
String[] part = line.split(":");
String part1 = part[0].trim();
String part2 = part[1].trim();

Input : 81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)

Output :

totalWeight = 81;
index[] = {1,2,3,4,5,6}; 
weight[] = {53.38,88.62,78.48,72.30,30.18,46.34};  
value[] = {45,98,3,76,9,48}; 

The second String contains 3 values inside brackets. First number is "Index", second number is "Weight" and third number is "Value".
How do i split it and store it into 3 different arrays?

I know one way of doing it as parsing it into an object of a class that has 3 arrays and assign them values.
But is there any better way to do so?

Upvotes: 2

Views: 119

Answers (4)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60026

You can solve your problem using Patterns like this :

String str = "81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) "
    + "(4,72.30,$76) (5,30.18,$9) (6,46.34,$48)";

String regex = "\\((\\d+),(\\d+\\.\\d+),([^\\d](\\d+))\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
double totalWeight = Double.parseDouble(str.replaceAll("^(\\d+).*", "$1"));
List<Integer> indexes = new ArrayList<>();
List<Double> weights = new ArrayList<>();
List<Integer> valuess = new ArrayList<>();

while (matcher.find()) {  
    indexes.add(Integer.parseInt(matcher.group(1)));
    weights.add(Double.parseDouble(matcher.group(2)));
    valuess.add(Integer.parseInt(matcher.group(4)));
}

System.out.println(totalWeight);// 81.0
System.out.println(indexes);    // [1, 2, 3, 4, 5, 6]
System.out.println(weights);    // [53.38, 88.62, 78.48, 72.3, 30.18, 46.34]
System.out.println(valuess);    // [45, 98, 3, 76, 9, 48]
  • The total weight is the first number in the input for that you can get it using just replaceAll then parse the input Double.parseDouble(str.replaceAll("^(\\d+).*", "$1"));
  • to get the rest information you can use this regex \((\d+),(\d+\.\d+),([^\d](\d+))\) (regex demo) which can match the three different numbers between two (), in your case you have many results for that you can use Patterns.

Upvotes: 1

J. D.
J. D.

Reputation: 1571

String[] part2 = part[1].replace("(", "").trim().split(")");
List<String> indices = new ArrayList<String>();
List<String> weights= new ArrayList<String>();
List<String> values = new ArrayList<String>();

for (String s : part2)
{
   String item = s.split(",");
   indices.add(item[0]);
   weights.add(item[1]);
   values.add(item[2]);
}

// to get them as arrays
String[] allWeightsArray = weights.toArray(new String[]); ...

Upvotes: 1

Ali Faris
Ali Faris

Reputation: 18610

use split method to split string by [\(?+\\)]+\s+

String x = part[1].trim(); 
String[] items = x.split("[\\(?+\\)]+\\s+");
List<String> indexs = new ArrayList<>();
List<String> wights = new ArrayList<>();
List<String> values = new ArrayList<>();
for(String item : items)
{
    item = item.replace("(" , "").replace(")" , "").trim();
    String[] partItems = item.split(",");
    indexs.add(partItems[0]);
    wights.add(partItems[1]);
    values.add(partItems[2]);
}

System.out.println(Arrays.toString(indexs.toArray()));
System.out.println(Arrays.toString(wights.toArray()));
System.out.println(Arrays.toString(values.toArray()));

output

[1, 2, 3, 4, 5, 6]
[53.38, 88.62, 78.48, 72.30, 30.18, 46.34]
[$45, $98, $3, $76, $9, $48]

Upvotes: 1

Ruslan K.
Ruslan K.

Reputation: 142

Use this code to parse string in this format:

String[] structured = part2.replaceAll("[()]", "").split(" ");

List<String> indexes = new ArrayList<>();
List<String> weights = new ArrayList<>();
List<String> values = new ArrayList<>();

for (String elem : structured) {
    String[] parsed = elem.split(",");
    indexes.add(parsed[0]);
    weights.add(parsed[1]);
    values.add(parsed[2]);
}

Upvotes: 1

Related Questions