Nth occurrence of a character and if conditionals - java

Let's say we have two strings (ULRs):
https://stackoverflow.com/questions/ask
https://web.whatsapp.com/
I need to write expression such as:
if after 3rd slash (/) is nothing or 3rd slash does not exists do
{
some operation
} else {
another action
}
Please help.
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}
String[] stringArr = list.toArray(new String[0]);
//copying and removing https from the list
List<String> list2 = new ArrayList<String>();
Collections.addAll(list2, stringArr);
Iterator<String> iter = list2.iterator();
while(iter.hasNext()){
if(iter.next().contains(https))
// here you should copy https lines to another file.
iter.remove();
}
String[] stringArr2 = list2.toArray(new String[0]);
for (int i = 0; i<stringArr2.length; i++) {
//condition for pure domain names.
//else the action below
System.out.println(getDomainName(stringArr2[i]) + "," + stringArr2[i] + "," + "U" +"," + number_of_doc + "," + today);
}
}
public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
}

Why you don't split :
String link = "https://stackoverflow.com/questions/ask ";
if (link.split("/").length >= 3 ) {
System.out.println("CORRECT");
}else{
System.out.println("NOT CORRECT");
}
The idea is : Split your String with / if the result is great or equal then 3 then your condition is correct, else not correct.
Edit
Or like #biziclop mention in comment you can use Guava's Splitter for example :
Iterable<String> result =
Splitter.on(CharMatcher.anyOf("/")).
limit(4).
split("https://stackoverflow.com/questions/ask");
if (Lists.newArrayList(result).size() > 3) {
System.out.println(Lists.newArrayList(result).get(3));
}else{
System.out.println("NOTHING");
}
Input
https://stackoverflow.com/questions/ask
https://stackoverflow.com
Output
questions/ask
NOTHING

You can use simple regex:
String url = "https://web.whatsapp.com/";
if(url.matches("\\w+://(\\w+.\\w+)+(.\\w+)*/(.+)"))
System.out.println("Correct URL");
else
System.out.println("Incorrect URL");

Related

How can I scope three different conditions using the same loop in Java?

I would like to count countX and countX using the same loop instead of creating three different loops. Is there any easy way approaching that?
public class Absence {
private static File file = new File("/Users/naplo.txt");
private static File file_out = new File("/Users/naplo_out.txt");
private static BufferedReader br = null;
private static BufferedWriter bw = null;
public static void main(String[] args) throws IOException {
int countSign = 0;
int countX = 0;
int countI = 0;
String sign = "#";
String absenceX = "X";
String absenceI = "I";
try {
br = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new FileWriter(file_out));
String st;
while ((st = br.readLine()) != null) {
for (String element : st.split(" ")) {
if (element.matches(sign)) {
countSign++;
continue;
}
if (element.matches(absenceX)) {
countX++;
continue;
}
if (element.matches(absenceI)) {
countI++;
}
}
}
System.out.println("2. exerc.: There are " + countSign + " rows int the file with that sign.");
System.out.println("3. exerc.: There are " + countX + " with sick note, and " + countI + " without sick note!");
} catch (FileNotFoundException ex) {
Logger.getLogger(Absence.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
text file example:
# 03 26
Jujuba Ibolya IXXXXXX
Maracuja Kolos XXXXXXX
I think you meant using less than 3 if statements. You can actually so it with no ifs.
In your for loop write this:
Countsign += (element.matches(sign)) ? 1 : 0;
CountX += (element.matches(absenceX)) ? 1 : 0;
CountI += (element.matches(absenceI)) ? 1 : 0;
Both answers check if the word (element) matches all regular expressions while this can (and should, if you ask me) be avoided since a word can match only one regex. I am referring to the continue part your original code has, which is good since you do not have to do any further checks.
So, I am leaving here one way to do it with Java 8 Streams in "one liner".
But let's assume the following regular expressions:
String absenceX = "X*";
String absenceI = "I.*";
and one more (for the sake of the example):
String onlyNumbers = "[0-9]*";
In order to have some matches on them.
The text is as you gave it.
public class Test {
public static void main(String[] args) throws IOException {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File txtFile = new File(desktop, "test.txt");
String sign = "#";
String absenceX = "X*";
String absenceI = "I.*";
String onlyNumbers = "[0-9]*";
List<String> regexes = Arrays.asList(sign, absenceX, absenceI, onlyNumbers);
List<String> lines = Files.readAllLines(txtFile.toPath());
//#formatter:off
Map<String, Long> result = lines.stream()
.flatMap(line-> Stream.of(line.split(" "))) //map these lines to words
.map(word -> regexes.stream().filter(word::matches).findFirst()) //find the first regex this word matches
.filter(Optional::isPresent) //If it matches no regex, it will be ignored
.collect(Collectors.groupingBy(Optional::get, Collectors.counting())); //collect
System.out.println(result);
}
}
The result:
{X*=1, #=1, I.=2, [0-9]=2}
X*=1 came from word: XXXXXXX
#=1 came from word: #
I.*=2 came from words: IXXXXXX and Ibolya
[0-9]*=2 came from words: 03 and 06
Ignore the fact I load all lines in memory.
So I made it with the following lines to work. It escaped my attention that every character need to be separated from each other. Your ternary operation suggestion also nice so I will use it.
String myString;
while ((myString = br.readLine()) != null) {
String newString = myString.replaceAll("", " ").trim();
for (String element : newString.split(" ")) {
countSign += (element.matches(sign)) ? 1 : 0;
countX += (element.matches(absenceX)) ? 1 : 0;
countI += (element.matches(absenceI)) ? 1 : 0;

Java .split() out of bounds

I have a problem with my code.
I'm trying to extract the name of the channels from a .txt file.
I can't understand why the method line.split() give me back an array with 0 length:
Someone can help me?
This is the file .txt:
------------[channels.txt]---------------------
...
#CH id="" tvg-name="Example1" tvg-logo="http...
#CH id="" tvg-name="Example2" tvg-logo="http...
#CH id="" tvg-name="Example3" tvg-logo="http...
#CH id="" tvg-name="Example4" tvg-logo="http...
...
This is my code:
try {
FileInputStream VOD = new FileInputStream("channels.txt");
BufferedReader buffer_r = new BufferedReader(new InputStreamReader(VOD));
String line;
ArrayList<String> name_channels = new ArrayList<String>();
while ((line = buffer_r.readLine()) != null ) {
if (line.startsWith("#")) {
String[] first_scan = line.split(" tvg-name=\" ", 2);
String first = first_scan[1]; // <--- out of bounds
String[] second_scan = first.split(" \"tvg-logo= ", 2);
String second = second_scan[0];
name_channels.add(second);
} else {
//...
}
}
for (int i = 0; i < name_channels.size(); i++) {
System.out.println("Channel: " + name_channels.get(i));
}
} catch(Exception e) {
System.out.println(e);
}
So you have examples like this
#CH id="" tvg-name="Example1" tvg-logo="http...
And are trying to split on these strings
" tvg-name=\" "
" \"tvg-logo= "
Neither of those strings are in the example. There's a spurious space appended, and the space at the start of the second is in the wrong place.
Fix the strings and here's a concise but complete program to demonstrate
interface Split {
static void main(String[] args) {
String line = "#CH id=\"\" tvg-name=\"Example1\" tvg-logo=\"http...";
String[] first_scan = line.split(" tvg-name=\"", 2);
String first = first_scan[1]; // <--- out of bounds
String[] second_scan = first.split("\" tvg-logo=", 2);
String second = second_scan[0];
System.err.println(second);
}
}
Of course, if you have any lines that start with '#' but don't match, you'll have a similar problem.
This sort of thing is probably done better with regexs and capturing groups.
There is a whitespace after the last double quote in tvg-name=\" which does not match the data in your example.
When you use split with line.split(" tvg-name=\"", 2) then the first item in the returned array will be #CH id="" and the second part will be Example1" tvg-logo="http..."
If you want to get the value of tvg-name= you might use a regex with a capturing group where you would capture not a double quote using a negated character class [^"]+
tvg-name="([^"]+)"
try {
FileInputStream VOD = new FileInputStream("channels.txt");
BufferedReader buffer_r = new BufferedReader(new InputStreamReader(VOD));
String line;
ArrayList<String> name_channels = new ArrayList<String>();
while((line = buffer_r.readLine()) != null ){
if(line.startsWith("#")){
String regex = "tvg-name=\"([^\"]+)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
name_channels.add(matcher.group(1));
}
} else {
// ...
}
}
for(int i = 0; i < name_channels.size(); i++){
System.out.println("Channel: " + name_channels.get(i));
}
}catch(Exception e){
System.out.println(e);
}

How can i split String in java with custom pattern

I am trying to get the location data from this string using String.split("[,\\:]");
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
String[] str = location.split("[,\\:]");
How can i get the data like this.
str[0] = 27.980194
str[1] = 46.090199
str[2] = 0.48
str[3] = 1
str[4] = 6
Thank you for any help!
If you just want to keep the numbers (including dot separator), you can use:
String[] str = location.split("[^\\d\\.]+");
You will need to ignore the first element in the array which is an empty string.
That will only work if the data names don't contain numbers or dots.
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
Matcher m = Pattern.compile( "\\d+\\.*\\d*" ).matcher(location);
List<String> allMatches = new ArrayList<>();
while (m.find( )) {
allMatches.add(m.group());
}
System.out.println(allMatches);
Quick and Dirty:
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
List<String> strList = (List) Arrays.asList( location.split("[,\\:]"));
String[] str = new String[5];
int count=0;
for(String s : strList){
try {
Double d =Double.parseDouble(s);
str[count] = d.toString();
System.out.println("In String Array:"+str[count]);
count++;
} catch (NumberFormatException e) {
System.out.println("s:"+s);
}
}

How to remove logical error

I want to print the all values in the array but it just prints the last value int the array, how can I get my desired result by improving this code:
public void applyAttendence(ArrayList<String> presents, ArrayList<String> absents) {
ArrayList<String> present = new ArrayList<String>();
HashMap params = new HashMap();
// [232, 232, 12, 223]
String[] stringArray = presents.toArray(new String[0]);
if (presents.size() == 0) {
params.put("present", "");
} else {
// for(String pre:presents) {
params.put("present", stringArray);
System.out.println(" present[]" + presents);
System.out.println("hellow present man: " + params.get("present"));
// }
System.out.println("hellow present man: " + params.get("present"));
}
if (absents.size() == 0) {
params.put("absent", "");
} else {
for (String abs : absents) {
params.put("absent[]", abs);
}
// params.put("present[]", presents + "");
//
params.put("absent[]", absents + "");
}
}
That is because you are overwriting same key with different value every time
for (String abs : absents) {
params.put("absent[]", abs);
}
So your hashmap will only have last value written against the key absent[]
This is may be you have defined array as:
String[] stringArray = presents.toArray(new String[0]);
try initializing as:
String[] stringArray = new String[presents.size()];
stringArray = presents.toArray(stringArray );
Try this simplified solution to show all of the attendance
public void applyAttendence(ArrayList<String> presents, ArrayList<String> absents) {
String sPresent = "";
for (String present : presents) {
sPresent += present + ", ";
}
if (!sPresent.equals(""))
sPresent = sPresent.substring(0, sPresent.length() - 2);
String sAbsent = "";
for (String absent : absents) {
sAbsent += absent + ", ";
}
if (!sAbsent.equals(""))
sAbsent = sAbsent.substring(0, sAbsent.length() - 2);
if (presents.size() > 0) {
System.out.println("present = " + sPresent);
} else {
System.out.println("present = no one");
}
if (absents.size() > 0) {
System.out.println("absent = " + sAbsent);
} else {
System.out.println("absent = no one");
}
}

Reading file and store in vector

I'm reading from the file:
name1 wordx wordy passw1
name2 wordx wordy passw2
name3 wordx wordy passw3
name (i) wordx wordy PASSW (i)
x
x word
x words
words
x
words
At the moment I can print line by line:
Line 1: name1 wordx wordy passw1
Line 2: name2 wordx wordy passw2
I plan to have access to:
users [0] = name1
users [1] = name2
users [2] = name3
..
passws [0] = passw1
passws [1] = passw2
passws [2] = passw3
..
My code is:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine()+",");
}
String listString = "";
for (String s : list) {
listString += s + "\t";
}
String[] parts = listString.split(",");
System.out.println("Line1: "+ parts[0]);
}
How do I get the following output:
User is name1 and password is passw1
User is name32 and password is passw32
Thanks in advance.
Something like this will do:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine());
}
int line = 0;
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
EDIT: if you want to iterate through all lines just put last lines in a loop:
for (int line = 0; line < list.size(); line++) {
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
First thing to do is, to add this loop to the end of your code :
for(int i = 0; i <= parts.length(); i++){
System.out.println("parts["+i+"] :" + parts[i] );
}
that will simply show the result of the split using ,.
Then adapt your code, you may want to use another regex to split() your lines, for instance a space.
String[] parts = listString.split(" ");
for documentation about split() method check this.
If you want to get that output then this should do the trick:
public static void main(String args[]) throws FileNotFoundException, IOException {
Scanner inFile = null;
try {
inFile = new Scanner(new File("F:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Map<String, String> userAndPassMap = new LinkedHashMap<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
userAndPassMap.put(userAndPass[0], userAndPass[1]);
}
for (Map.Entry<String, String> entry : userAndPassMap.entrySet()) {
System.out.println("User is:" + entry.getKey() + " and password is:" + entry.getValue());
}
}
By storing in a map you are linking directly each username with its password. If you need to save them into separate arrays then you can do this in the while loop instead:
List<String> users = new LinkedList<>(),passwords = new LinkedList<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
users.add(userAndPass[0]);
passwords.add(userAndPass[1]);
}
and later transform them to arrays
users.toArray()
I recommend you use a java.util.Map, a standard API which allows you to store objects and read each one of them by a key. (In your case, string objects indexed by string keys). Example:
Let's assume this empty map:
Map<String, String> map=new HashMap<String,String>();
If you store this:
map.put("month", "january");
map.put("day", "sunday");
You can expect that map.get("month") will return "january", map.get("day") will return "sunday", and map.get(any-other-string) will return null.
Back to your case: First, you must create and populate the map:
private Map<String, String> toMap(Scanner scanner)
{
Map<String, String> map=new HashMap<String, String>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
map.put(parts[0], parts[parts.length-1]);
}
}
return map;
}
And then, to read the map:
private void listMap(Map<String,String> map)
{
for (String name : map.keySet())
{
String pass=map.get(name);
System.out.println(...);
}
}
You must include both in your class and call them from the main method.
If you need arbitraray indexing of the read lines, use ArrayList:
First, define a javabean User:
public class User
{
private String name;
private String password;
// ... add full constructor, getters and setters.
}
And then, you must create and populate the list:
private ArrayList<User> toList(Scanner scanner)
{
List<User> list=new ArrayList<User>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
list.add(new User(parts[0], parts[parts.length-1]));
}
}
return list;
}

Categories