How to extract polynomial coefficients in Java? - java

Taking the string -2x^2+3x^1+6 as an example, how how to extract -2, 3 and 6 from this equation stored in the string?

Not giving the exact answer but some hints:
Use replace meyhod:
replace all - with +-.
Use split method:
// after replace effect
String str = "+-2x^2+3x^1+6"
String[] arr = str.split("+");
// arr will contain: {-2x^2, 3x^1, 6}
Now, each index value can be splitted individually:
String str2 = arr[0];
// str2 = -2x^2;
// split with x and get vale at index 0

String polynomial= "-2x^2+3x^1+6";
String[] parts = polynomial.split("x\\^\\d+\\+?");
for (String part : parts) {
System.out.println(part);
}
This should work. Sample output
polynomial= "-2x^2+3x^1+6"
Output:
-2
3
6
polynomial = "-30x^6+20x^3+3"
Output:
-30
20
3

Related

How to get number from string array in java

i have a string like String[] str = {"[5, 2, 3]","[2, 2, 3, 10, 6]"} and i need to take numbers to add into an integer list.
i tried to split first index into numbers to see if it will work, looks like:
String[] par = str[0].split("[, ?.#]+");
After the split i tried to see what array i get:
for(String a: par)
System.out.println(a);
But when i wrote that code i get an array like this:
[5
2
3]
So, how can i get rid of this square brackets?
Instead of your current pattern, I would use \\D+ which will split on one or more non-digits. Add a guard for the empty string too. Something like
String[] str = { "[5, 2, 3]", "[2, 2, 3, 10, 6]" };
for (String par : str) {
for (String t : par.split("\\D+")) {
if (t.isEmpty()) {
continue;
}
System.out.println(Integer.parseInt(t));
}
}
Outputs
5
2
3
2
2
3
10
6

Java: How do you accept an integer array without using a for loop?

Is it possible to accept an integer array from user without using a for loop?
input will be :
4 //size
1 2 3 4 //array values
String stdin="1 2 3 4";
String str[]= stdin.split(" ");
int st[] = Integer.parseInt(stdin.split(" "));
this code does not work though
You can do this without a loop in Java 8 using stream.
Here is what you're looking for:
String str = "1 2 3 4";
int[] arr = Arrays.stream(str.split("\\s+"))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
Goodluck, thanks.

Java, How to convert a string into an Array

I've got a string, for example:
String code = new String("[199, 56, 120]")
My goal is to create and Array that contains only the numbers inside the [] and beetween commas;
In this case it would be for example:
array[0] = 199
array[1] = 56
array[2] = 120
Is possible to do something like this??
Thanks in advance.
You just need to use the split function.
Skip the [ and the ] and you can do something like this:
String input = "199 56 120";
String[] array = input.split(" ");
If you really want [ and the ] then you can use something like
input.replace("[", "");
input.replace("]", "");
To strip the string before you split it.
Edit
It doesn't matter what the format is or what the numbers are or how many they are, you simply edit the split definition according to the format, so if you're case is , then you simply use that as the split parameter.
String input = "[number, number, number]";
String sep = ", ";
String fixedInput = input.replace("[", "").replace("]", "");
String[] array = fixedInput.split(sep);
// array[0] contains first number.
// array[1] contains second number.
If you want an int[] array then you could do something like this:
int[] intArray = new int[array.length];
for(int i = 0; i < array.length; i++)
{
intArray[i] = Integer.parseInt(array[i]);
}
If you want a one-line solution:
String[] array = code.replaceAll("[^\\d ]", "").split(" ");
The in-line call to replaceAll() removes non-digits/spaces.

Java String to Array

I want to evaluate String like "[1,5] [4,5] [10,6]" in in array.
I'm not quite familiar with the Java regex and the syntax.
String game = "[1,5] [4,5] [10,6]"
Pattern splitter = Pattern.compile("\\[|,|\\]");
splitter.matcher(game);
public String [] gameArray = null;
gameArray = splitter.split(game);
I want to to iterate over each pair of array such as : [0][0] => 1; [0][1] => 5
If you put
StringTokenizer st = new StringTokenizer(game, "[,] ");
while (st.hasMoreTokens())
{
currentNumber = Integer.parseInt(st.nextToken());
// fill array with it
}
It should be what you need, if I understood well
For this purpose you need to split your string.
first of all you need to split on space and after that you need to split on ,(comma).and your third step will be remove brackets So at the end you will get you string into array.
Try,
String game = "[1,5] [4,5] [10,6]";
String[] arry=game.substring(1, game.length()-1).split("\\] +\\[");
List<String[]> twoDim=new ArrayList<>();
for (String string : arry) {
String[] twoArr=string.split(",");
twoDim.add(twoArr);
}
String[][] twoArr=twoDim.toArray(new String[0][0]);
System.out.println(twoArr[0][0]); // 1
System.out.println(twoArr[0][1]); // 5

how to split this string[LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,] as my requirement in java

hello every one i got a string from csv file like this
LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,
how to split this string with comma i want the array like this
s[0]=LECT-3A,s[1]=instr01,s[2]=Instructor 01,s[3]=teacher,s[4]=instr1#learnet.com,s[5]=,s[6]=,s[7]=,s[8]=male,s[9]=phone,s[10]=,s[11]=
can anyone please help me how to split the above string as my array
thank u inadvance
- Use the split() function with , as delimeter to do this.
Eg:
String s = "Hello,this,is,vivek";
String[] arr = s.split(",");
you can use the limit parameter to do this:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
Example:
String[]
ls_test = "LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,".split(",",12);
int cont = 0;
for (String ls_pieces : ls_test)
System.out.println("s["+(cont++)+"]"+ls_pieces);
output:
s[0]LECT-3A
s[1]instr01
s[2]Instructor 01
s[3]teacher
s[4]instr1#learnet.com
s[5]
s[6]
s[7]
s[8]male
s[9]phone
s[10]
s[11]
You could try something like so:
String str = "LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,";
List<String> words = new ArrayList<String>();
int current = 0;
int previous = 0;
while((current = str.indexOf(",", previous)) != -1)
{
words.add(str.substring(previous, current));
previous = current + 1;
}
String[] w = words.toArray(new String[words.size()]);
for(String section : w)
{
System.out.println(section);
}
This yields:
LECT-3A
instr01
Instructor 01
teacher
instr1#learnet.com
male
phone

Categories