Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The variable productQR will have a string value of the format <rate>,<quantity>@<rate>,<quantity>@<rate>,<quantity>.
To give an example,
productQR = "250,10@100,3@50,7" will have the Rate as 250, 100 and 50 while Quantity as 10, 3 and 7 respectively. How can I assign those values to Rate1, Rate2 and Rate3 respectively? Same for the Quantity variable?

This is the function:
String calcGST(Receipt r) {
		String regex = "[\d]+[,]{1}[\d]+[@]{1}[\d]+[,]{1}[\d]+[@]{1}[\d]+[,]{1}[\d]+";
		Pattern p = Pattern.compile(regex);

		r.productQR = Rate + "," + q1 + "@" + Rate2 + "," + q2 + "@" + Rate3 + "," + q3; // I know this part is wrong, just for representation purpose.
		
		return null; //we have to finally return the variable in which we'll put those 6 variables for GST calculation. 

	}


this is the receipt class:
class Receipt {
	TransactionParty transactionParty;
	String productQR;

	public Receipt(TransactionParty transactionParty, String productQR) {
		super();
		this.transactionParty = transactionParty;
		this.productQR = productQR;
	}

}


What I have tried:

I tried using regex/.split() but I don't know how can we extract values from it.
Posted
Updated 29-Mar-22 21:55pm
v4

1 solution

I would use String (Java Platform SE 7 )[^] to break the sets into a simple array. Then use the same method on each string to separate rate and quantity. You can then use Integer (Java Platform SE 7 )[^] to convert each substring to its integer value.
 
Share this answer
 
Comments
Sourabh Jambale 30-Mar-22 5:07am    
I did for an example string:


String s1 = "250,10@100,3@50,7";
String hello[] = s1.split("@", 3);
String s2[] = null;
for (String a : hello) {
s2 = a.split(",");
for (String b : s2) {
int z = Integer.parseInt(b);
System.out.println(z);
}
}

All the integer values are stored in z but how can individually assign them to their respective variables, for example 250 to Rate1,100 to Rate 2 and so on?
Richard MacCutchan 30-Mar-22 5:46am    
Use an ArrayList<t> to hold the values. Also, remember that Integer.parseInt will throw an Exception if the strings contain non-numeric characters, so you should code for that.
So something like:
class Pair {
    // this is a convenience class to keep the two values together
    public int p1;
    public int p2;
    Pair(int v1, int v2)
    {
        p1 = v1;
        p2 = v2;
    }
}

// and then the main code

String s1 = "250,10@100,3@50,7";
List<Pair> numList = new ArrayList<Pair>();
String l1[] = s1.split("@", 3);
for (String a : l1) {
    String s2[] = a.split(",");
    try {
    int v1 = Integer.parseInt(s2[0]);
    int v2 = Integer.parseInt(s2[1]);
    numList.add(new Pair(v1, v2));
    }
    catch (NumberFormatException ne)
    {
        // do something here
    }
}
for (Pair p : numList) {
    System.out.printf("%d - %d%n", p.p1, p.p2);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900