I have been doing web-scraping for a project and I couldn't figure out how to split retrieved strings into different variables.
Array of strings I have retrieved
K. KAYMAKLI TSK 6 5 0 1 19 4 15 15
YEN?CAM? AK 6 4 1 1 14 7 7 13
MORMENEK?E GB 6 4 0 2 10 7 3 12
LEFKE TSK 6 3 2 1 10 8 2 11
SERDARLI GB 6 2 2 2 6 5 1 8
HAM?TKÖY ?HSK 6 2 2 2 8 8 0 8
ÇET?NKAYA TSK 6 2 2 2 6 7 -1 8
DO?AN TBSK 6 2 2 2 12 15 -3 8
YEN? BO?AZ?Ç? DSK 6 2 1 3 9 8 1 7
B. BA?CIL SK 6 1 4 1 8 9 -1 7
MA?USA TÜRK GÜCÜ 6 2 1 3 7 9 -2 7
C?HANG?R GSK 6 1 3 2 8 8 0 6
GENÇL?K GÜCÜ SK 6 1 0 5 6 14 -8 3
YALOVA SK 6 0 2 4 4 18 -14 2
I would like to put the characters until the first integer (in this case 6) into one string
and then each integer into separate variables.
eg.
String team = "YALOVA SK";
int p = 6;
int x = 0;
int y = 2;
int z = 4;
int m = 4;
int n = 18;
int k = -14;
int h = 2;
I was able to split the string by checking character by character to find the first integer and split it there and then assign each character after that to an integer. How can I solve it by using regex?
Note ?'s are turkish characters that are not displayed correctly on the console but are display correctly in the application.
The split() method on a string can be used to split the string based on a particular 'regex'. For reference on split function check Oracle java documentation link. There is also a nice tutorial about using regex in java in the Vogella website.
You can split the string based on '\s'(short white space character) or '\s+'and then use the returned array.
The syntax will be something like this.
String retrievedString = "YALOVA SK 6 0 2 4 4 18 -14 2";
String[] teamInfo=retrievedString.split("\\s");
You can use the string you retrieve from web scraping in place of "retrievedString".
Note the \\ used in the split method is to escape \.
Hope this helps...
I think Scanner class fit perfect to your needs:
Description: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Related
Edit: For some reason my code works as intended in an online compiler. But in eclipse, the exact same code has the following problem.
I am working on a Kattis Problem called Electrical Outlets.
On the first line of input will be the number of test cases (n). On the next n lines, each line will start with an integer k, which is the number of power strips that will follow on the line. The rest of the line will contain how many outlets each of these power strips contain. Here is my code:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
ArrayList<Integer> outlets = new ArrayList<>();
int testCases = scnr.nextInt();
int stripCount;
int appliances = 0;
for (int i = 0; i < testCases; i++) {
stripCount = scnr.nextInt();
for (int j = 0; j < stripCount; j++) {
outlets.add(scnr.nextInt());
appliances += outlets.get(j);
}
System.out.print("\nNumber of outlets: " + (appliances - (outlets.size() - 1)));
System.out.print("\n" + i);
outlets.clear();
appliances = 0;
}
}
Now for some reason, the outer for loop always runs one less time then it should.
Here is a sample input
3
3 2 3 4
10 4 4 4 4 4 4 4 4 4 4
4 10 10 10 10
And expected output
Number of outlets: 7
Number of outlets: 31
Number of outlets: 37
However, my output is
Number of outlets: 7
Number of outlets: 31
Similarly, for input
5
5 4 3 2 5 6
4 3 2 2 3
7 4 4 4 4 4 4 4
5 4 3 3 4 4
8 9 9 9 9 9 9 9 9
I expect an output of
Number of outlets: 16
Number of outlets: 7
Number of outlets: 22
Number of outlets: 14
Number of outlets: 65
But receive an output of
Number of outlets: 16
Number of outlets: 7
Number of outlets: 22
Number of outlets: 14
If I comment out the following two lines contained within for loop(int j)
for (int j = 0; j < stripCount; j++) {
//outlets.add(scnr.nextInt());
//appliances += outlets.get(j);
}
And add print(i), the for loop iterates as many times as it should.
Kattis validates your code by inspecting your program's standard output. If even a single character is different than the judge expects, your submission will fail, and Kattis will only report "Wrong answer" without any reason, actual/expected diff or other diagnostics.
Therefore, it's critical to pay close attention to the expected output format in the problem description and sample test cases.
For the first sample input, we're given:
3
3 2 3 4
10 4 4 4 4 4 4 4 4 4 4
4 10 10 10 10
And the expected output is:
7
31
37
But your output is different:
Number of outlets: 7
0
Number of outlets: 31
1
Number of outlets: 37
2
This contains unnecessary "Number of outlets: " prefixes as well as an i for each test case. Sure, to a human, this is fine, but Kattis' runner program isn't smart enough to understand that you basically got it right.
Removing the i print and changing your result print to match what Kattis is asking for passes the submission tests:
System.out.println(appliances - (outlets.size() - 1));
As for the missing last line, you're probably not flushing that final buffer in the IDE. Either add a final newline or press Enter manually if you have interactive capabilities. This appears to be an environment misunderstanding, not a code problem.
I am not sure what is wrong. I looked up other similar titled questions and answers on stack overflow but not sure what is wrong with my method. I am a beginner in java so any help would be great. Thank you.
My code is:
import java.util.*;
class LargestNumber {
static void printLargest(Vector<String> arr){
Collections.sort(arr, new Comparator<String>(){
#Override
public int compare(String X, String Y) {
String XY=X + Y;
String YX=Y + X;
return XY.compareTo(YX) > 0 ? -1:1;
}
});
Iterator it = arr.iterator();
while(it.hasNext())
System.out.print(it.next());
}
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
int i,n;
n=s.nextInt();
Vector<String> arr;
arr = new Vector<>();
for(i=0;i<n;i++){
arr.add(s.next());
}
printLargest(arr);
}
}
error:
100
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
Your output:
Your stderr:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:777)
at java.util.TimSort.mergeAt(TimSort.java:514)
at java.util.TimSort.mergeCollapse(TimSort.java:441)
at java.util.TimSort.sort(TimSort.java:245)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.Vector.sort(Vector.java:1345)
at java.util.Collections.sort(Collections.java:177)
at LargestNumber.printLargest(LargestNumber.java:6)
at LargestNumber.main(LargestNumber.java:32)
Correct output:
9999999998888888888887777777776666666666555555554444444443333333333222222222111111111111111101010101010101010
(Time used: 0.12/1.50, memory used: 23568384/536870912.)`enter code here`
Problem is that your statement XY.compareTo(YX) > 0 ? -1:1 does not return 0 when two variables are equal, which is IllegalArgumentException: Comparison method violates its general contract!
From doc:
Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns: a negative integer, zero, or a positive integer as the first
argument is less than, equal to, or greater than the second.
Use following code to compare:
arr.sort((X, Y) -> {
String XY = X + Y;
String YX = Y + X;
return XY.compareTo(YX);
});
What's happening is that you are entering
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
with spaces. When that happens, the input gets treated like a one string without another string to compare to, which is why you got IllegalArgumentException. To fix this code, change your for loop in your main method like this...
for(i=0;i<n;i++){
arr.add(String.valueOf(new Random().nextInt(17)));
}
I was in an interview today and I got the following exercice :
write a program to find all the 3x3 matrix X for each element x varying from 0 to 100
A B C
D E F
G H I
that meet the following requirements
A + B - C = 4
+ - -
D - E * F = 4
/ * -
G + H + I = 4
= = =
4 4 4
write a program in Java.
It is not really clear what your question is, but this seems quite straightforward to simply bruteforce by trying all choices in a sensible order.
For example, this Python code:
for G in range(1,4+1):
for H in range(4+1-G):
I = 4 - H - G
for A in range(0,4+1):
D = G*(4-A)
if not 0<=D<=100:
continue
for E in range(100+1):
for F in range(100+1):
if D-E*F==4:
for B in range(100+1):
C=A+B-4
if 0<=C<=100:
if B-E*H==4:
if C-F-I==4:
print A,B,C
print D,E,F
print G,H,I
print A+B-C,D-E*F,G+H+I,A+D/G,B-E*H,C-F-I
finds the following 4 solutions:
0 10 6
4 6 0
1 1 2
4 4 4 4 4 4
2 7 5
4 3 0
2 1 1
4 4 4 4 4 4
1 8 5
6 2 1
2 2 0
4 4 4 4 4 4
2 6 4
4 1 0
2 2 0
4 4 4 4 4 4
Okay so I need to make a number pattern with day numbers for example: 1 - monday, 2 - tuesday, 3 - wednesday until 7 - sunday. If I put an input "n" I would get the following:
n=4
1 2 3 4
n=7
1 2 3 4 5 6 7
n=12
1 2 3 4 5 6 7 1 2 3 4 5
I've succeeded making this program if n<=14 but if n>14 I get:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10
when it should be:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
this is my code
for (x=1;x<=n;x++){
System.out.print(x+" ");
if (x==7){
for (x=1;x<=(n-7);x++)
System.out.print(x+" ");
break;
}
}
thanks in advance
Try this instead:
for (int i = 0; i < n; i++)
System.out.print(i % 7 + 1 + " ");
Whenever you want to have that "repeating" behavior, where a sequence of numbers goes up to a certain value and then restarts, use the % operator and a bit of modular arithmetic to achieve the desired effect. For n = 17 the above will print:
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
I have similar problem with little changes, which is:
I have text file contain large number of lines with different sizes "i.e. not all lines has same length"
each line contain integers only.
as an example A.txt =
4 6 4 1 2 2 5 7 7
0 9 5 5 3 2 43 3 32 9 0 1 3 1
3 4 5 6 7 4
34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8 4 2 1 5
I want to put these integers into an array so each integer will be an element in the array and saving lines from "overlapping" i.e. I need to keep each line as it is.
Could anybodyy help me with this?
a = dlmread('a.txt')
a =
Columns 1 through 21
4 6 4 1 2 2 5 7 7 0 0 0 0 0 0 0 0 0 0 0 0
0 9 5 5 3 2 43 3 32 9 0 1 3 1 0 0 0 0 0 0 0
3 4 5 6 7 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8
Columns 22 through 25
0 0 0 0
0 0 0 0
0 0 0 0
4 2 1 5
I would do the following:
1) Create a new Array for each line
2) Read lines, one at a time from file
3) Split each line by the "space" char
4) Iterate through the String[] that you get from the split operation, passing each value to Integer.parseInt(value);
5) Store value in array;
6) When reading next line, create new array to store the new lines values.
You can read the data using Scanner, one line at a time, and store the numbers in a List, e.g. an ArrayList:
import java.util.*;
import java.io.*;
public class Numbers
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("A.txt"));
List<List<Integer>> ints = new ArrayList<List<Integer>>();
while (data.hasNextLine()) {
List<Integer> lineInts = new ArrayList<Integer>();
Scanner lineData = new Scanner(data.nextLine());
while (lineData.hasNextInt()) {
lineInts.add(lineData.nextInt());
}
ints.add(lineInts);
}
System.out.println(ints);
}
}
This code opens the file for reading, and creates a two-dimensional ArrayList. The outer list contains a list for each line in the file. The inner lists contain the integers on the respective lines. Note that empty lines result in empty lists. Also, you will have to properly handle any IO exception, unlike the code shown above.
If you really want the integers in a two dimensional array instead of an ArrayList, then you'll either have to call toArray, or alter the code above. That is left as an exercise for the reader.