Split string based on interval [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split my strings in JAVA based on a regular interval, not on regex. This is what I have to split:
1 x3.1.105.41 1 -10
2 x4.1.105.41 0 -10
3 x12.1.105.41 0 -10
4 y3.1.105.41.19 1 0
5 y4.1.105.41.21 0 0
6 y1.1.105.41.23 0 0
7 y12.1.105.41.25 0 0
I would like to seperate each column. Currently, I use the strLine.spli function
Any help would be great!

You can use substring:
String myLine = "1 x3.1.105.41 1 -10";
String column1 = myLine.substring(0, 2).trim();
String column2 = myLine.substring(2, 20).trim();
...
Or just split the lines:
String myLine = "1 x3.1.105.41 1 -10";
String[] columns = myLine.trim().split("\\s+");
which gives you in columns[0] your first value, in `columns[1]ยด your second and so on.
The second solution looks smarter to me.

Related

Need to extract a specific number from a string with several numbers using Selenium [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to get 293 as an int from the string "1 to 20 of 293 total". I'm unable to do it using .replaceAll method. Please help
try this :
String s = "1 to 20 of 88 total";
String k [] = s.split("1 to 20 of");
String t [] = k[1].split("total");
System.out.println(Integer.valueOf(t[0].replaceAll("\\s+","")));

Print 2 ArrayLists Side by Side [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have created 2 ArrayLists of the same size (5) and i want to print out their contents side by side in columns.
For example:
list 1 = [1 , 2 , 3 , 4 , 5]
list 2 = [5 , 4 , 3 , 2 , 1]
I want to print it out to the console like this:
1 5
2 4
3 3
4 2
5 1
I have tried using a for loop within another for loop but I think I may be overthinking it.
Since, you need the lists to print side by side nested loops are not required.
for (int i = 0 ; i < list1.size(); i++) {
System.out.printf("%d\t%d\n", list1.get(i), list2.get(i));
}
Notice, the loop assumes that the two lists are of the same size. So, we need only one loop counter.
Output :
1 5
2 4
3 3
4 2
5 1

Find number string in string and Count [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table like this. In a String like this 01-10-33 I'd like to know how many times which number appears in my table. For example number 01 I wanna check if it is in first row, count and check line two,three...EOF and get the count number. In the example link I tried to check if the number was in the string without success. I know I could probably get all my numbers and do this in java, but is it possible to get this with sql?
The expected results in the example would be
numbers-count
01 - 1
02 - 2
03 - 2
05 - 1
10 - 2
12 - 1
If the numbers are always in that format you could use SUBSTRING to get all the parts out and count them:
select number, count(*)
from (
select substring(n, 1, 2) as number
from l
union all
select substring(n, 4, 2)
from l
union all
select substring(n, 7, 2)
from l
) a
group by number;
sqlfiddle demo
If you want to know how many times "01" appears in the string "01-10-33", you can use:
select l.*,
(length(n) - length(replace(n, #n, '')))/length(#n)
from l cross join
(select #n := '01') const;
Note that this does not handle delimiters. So, '01' will match '001'. If that requirement is important:
select l.*,
(length(n)+2 - length(replace(concat('-', n, '-'), #n, '')))/length(#n)
from l cross join
(select #n := '-01-') const;

How get the mod inverse [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The expression is: n mod m = x
I want to know the value of n given m and x
Is possible to get this value ? Is there is a java function to get that number?
It isn't possible to get this value. There are in fact multiple possibilities for a given m and x. For example, take n mod 3 = 1. We know that m is 3 and x is 1, but just knowing that, we don't know whether n is 4 or 7 or 10 or 13 or any other number that is one more than a multiple of three.
I don't think that can be done - look at a few examples
10 mod 9 = 1
19 mod 9 = 1
n could be 10, 19, 28 etc...
or
9 mod 8 = 1
17 mod 8 = 1
n could be 9, 17, 25 etc...

can't work out answer for simple practice quiz, looked in book/online can't find answer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
int[] x = {1, 2, 3, 4};
System.out.println(x[x[3-2]]);
can some one please explain what is going on?
can't find anywhere in my text book or online for an explanation
3-2 == 1, x[1] == 2; x[2] == 3.
That is, you first evaluate the expression 3-2. Then you evaluate the expression x[1], and so on, up the "levels of nestedness".
3 - 2 = 1
x[3-2] = x[1] = 2
x[x[3-2]] = x[2] = 3
So output should be "3"
starts out 3-2 = 1
so x[1] = 2 (zero based index)
which means you then have x[2] which is 3
so.. x[x[3-2]] = x[x[1]] = x[2] = 3

Categories

Resources