The third iteration of my for loop is only running once when it should be running twice.
First I have this array of words
String [] words = {"Lorem","ipsum","dolor","sit","amet","consectetur","adipiscing","elit.",,,,}
spaceCount = 7;
wordCount = 4;
while(wordCount>-1){
output+=words[start];
System.out.println("output:"+output);
System.out.println("spaceCount:"+spaceCount);
System.out.println("wordCount:"+wordCount);
start++;
System.out.println("j<:"+Math.ceil((double)spaceCount/(double)wordCount));
for(j=0;j<Math.ceil((double)spaceCount/(double)wordCount);j++){
System.out.println("j:"+j);
output+=" ";
spaceCount--;
}
wordCount--;
}
I tried to typecast the Math ceil from double to int but it still produce the same result.
The result that appear is:
output:Lorem
spaceCount:7
wordCount:4
j<:2.0
j:0
j:1
output:Lorem ipsum
spaceCount:5
wordCount:3
j<:2.0
j:0
j:1
output:Lorem ipsum dolor
spaceCount:3
wordCount:2
**j<:2.0
j:0**
output:Lorem ipsum dolor sit
**spaceCount:2
wordCount:1**
j<:2.0
j:0
output:Lorem ipsum dolor sit amet,
but it should be:
output:Lorem ipsum dolor
spaceCount:3
wordCount:2
j<:2.0
j:0
j:1
output:Lorem ipsum dolor sit
**spaceCount:1
wordCount:1
j<:1.0**
j:0
output:Lorem ipsum dolor sit amet,
Related
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I am trying to split a string to List where the delimiter is the $_$.
For example the text
Lorem ipsum dolor $$ sit amet, consectetur adipiscing $$ elit. Aliquam $_$ eu.
I would like to convert to o list with elements
el1= Lorem ipsum dolor
el2= sit amet, consectetur adipiscing
el3- elit. Aliquam
el4= eu.
I tried the code bellow with no success.Is there any other way?
List<String> myList = new ArrayList<String>(Arrays.asList(s.split("$_$")));
List<String> myList = new ArrayList<>(Arrays.asList(s.split("\\$_\\$")));
List<String> myList = Arrays.asList(s.split("\\$_\\$")); // or simply this
As $ has a special meaning (end-of-text) you need to regex-escape it by a backslash. In a String literal a backslash has to be escaped itself - with a backslash.
What I am trying to achieve is to match all words in text, but ignore those words in line (before new line) that start with 4 whitespaces.
Example
Text file to find words:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
This must NOT be matched. Because it has 4 whitespaces at the beginning.
Lorem ipsum dolor sit amet. Ut enim ad minim veniam.
So, the words in following line should be NOT considered to match pattern:
This must NOT be matched. Because it has 4 whitespaces at the beginning.
Code
Here is my regex and it can find all words:
\\b[A-Za-z]+\\b
I know that in Java's RegEx syntax there is except which is ^ symbol but I only know how to use it in more simple expressions.
Maybe following snippet could be a basis for what you want to achieve.
String[] lines = {"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut",
"enim ad minim veniam, quis nostrud exercitation ullamco laboris",
"nisi ut aliquip ex ea commodo consequat.",
"",
" This must NOT be matched. Because it has 4 whitespaces at the beginning.",
"",
"Lorem ipsum dolor sit amet. Ut enim ad minim veniam."};
for (String line : lines) {
if (!line.startsWith(" ")) {
String[] words = line.split("[\\p{IsPunctuation}\\p{IsWhite_Space}]+");
System.out.println("words = " + Arrays.toString(words));
}
}
output
words = [Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, sed, do]
words = [eiusmod, tempor, incididunt, ut, labore, et, dolore, magna, aliqua, Ut]
words = [enim, ad, minim, veniam, quis, nostrud, exercitation, ullamco, laboris]
words = [nisi, ut, aliquip, ex, ea, commodo, consequat]
words = []
words = []
words = [Lorem, ipsum, dolor, sit, amet, Ut, enim, ad, minim, veniam]
PS: the regex has been borrowed from this answer
The following should do that
(?<!\s{4})\\b[A-Za-z]+\\b
It begins with a negative lookbehind so it won't match anything with \s{4} preceding it.
oI have a Java proberties file which looks more or less like this:
FD6AEA14B3581255C5D40451CDFF8168.hash=90AD759FF0B41ABD7260EF1044E75330
FD6AEA14B3581255C5D40451CDFF8168.path=Volumes/UA08154711/08154711/Lorem ipsum dolor sit amet, consetetur sadipscing/Lorem ipsum dolor sit amet, consetetur sadipscing/07 - Lorem ipsum dolor sit amet, consetetur sadipscing - Lorem ipsum dolor sit amet, consetetur sadipscing (Album Version).mp3
EA3F9134319E314BC85D59D16122800.fileName=04 - Lorem ipsum dolor sit amet, consetetur sadipscing (Album Version).mp3
EA3F9134319E314BC85D59D16122800.hash=88302129514633AAED4553F1B0CCB6B8
EA3F9134319E314BC85D59D16122800.path=Volumes/UA08154711/08154711/Lorem ipsum dolor sit amet, consetetur sadipscing/Lorem ipsum dolor sit amet, consetetur sadipscing/04 - Lorem ipsum dolor sit amet, consetetur sadipscing (Album Version).mp3
EAFB12EE4094D48A2B1BD367E5737C80.fileName=._02 - Lorem ipsum dolor sit amet, consetetur sadipscing (Explicit Version).mp3
EAFB12EE4094D48A2B1BD367E5737C80.hash=34FB1D1E3523334A89D03DE707C00968
EAFB12EE4094D48A2B1BD367E5737C80.path=Volumes/UA08154711/08154711/Lorem ipsum dolor sit amet, consetetur sadipscing/Lorem ipsum dolor sit amet, consetetur sadipscing/._02 - Lorem ipsum dolor sit amet, consetetur sadipscing (Explicit Version).mp3
fileSize=1024595340
Number=03173729
the files destination is files/03173729.meta
I want to parse the file with php so I can save the data in the mySQL database. The code:
$filepath = "files/03173729.meta";
$fileContents = file_get_contents("$filepath");
echo $fileContents;
$result = parse_properties($fileContents);
//Fetch the contents from the result array and add them to the database
function parse_properties($txtProperties) {
$result = array();
$lines = split("\n", $txtProperties);
$key = "";
$isWaitingOtherLine = false;
foreach ($lines as $i => $line) {
if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0))
continue;
if (!$isWaitingOtherLine) {
$key = substr($line, 0, strpos($line, '='));
$value = substr($line, strpos($line, '=')+1, strlen($line));
}
else {
$value .= $line;
}
/* Check if ends with single '\' */
if (strrpos($value, "\\") === strlen($value)-strlen("\\")) {
$value = substr($value,0,strlen($value)-1)."\n";
$isWaitingOtherLine = true;
}
else {
$isWaitingOtherLine = false;
}
$result[$key] = $value;
unset($lines[$i]);
}
return $result;
}
But I do not know how to implement the file path into the code.
You can do like this:
<?php
$filepath = "[your path]";
$fileContents = file_get_contents("$filepath");
$result = parse_preperties($fileContents);
//Fetch the contents from the result array and add them to the database
function parse_properties($txtProperties) {
...
...
...
}
?>
I want to write a method in Java that splits a String by XML tags like follows:
"Lorem ipsum <b>dolor</b> sit amet consetetur <b>diam</b> nonumy."
Should return the Array:
["Lorem ipsum ", "<b>dolor</b>", " sit amet consetetur ", "<b>diam</b>", " nonumy."]
This should work for every XML tag, also self closing tags like <element />.
Is there a library that does something similar in a simple way?
Thanks!
Using lookaround in your split should do the trick:
String splits[] = input.split("\\s+(?=<b>)|(?<=</b>)\\s+");
Example:
String input = "Lorem ipsum <b>dolor</b> sit amet consetetur <b>diam</b> nonumy.";
for(String s : input.split("\\s+(?=<b>)|(?<=</b>)\\s+")){
System.out.println(s);
}
If you want to keep the space intact in your spitted array, then remove the \\s+ from the regex.
I have a text String, in this form
Lorem ipsum dolor sit amet,
consectetuer adipiscing elit
,
lo
sed diam
nonummy nibh
quis
nostrud exerci.
So looks realy bad when I set the text in a textView.
I need that the String is loaded in this form
Lorem ipsum dolor sit amet,
consectetuer adipiscing elit,
lo sed diam nonummy nibh quis
nostrud exerci.
Filling all the row (when is possible) before start new line.
Since cannot edit all the db entries to adjust the text.
Use this code to remove all new line special characters in the text
yourstring.replaceAll("[\n\r]", "")