Merge branch 'master' of github.com:eugenp/tutorials
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
## Relevant Articles:
|
||||
|
||||
- [Converting Java String to Double](https://www.baeldung.com/java-string-to-double)
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.string.wordcount;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class WordCounter {
|
||||
static final int WORD = 0;
|
||||
static final int SEPARATOR = 1;
|
||||
|
||||
public static int countWordsUsingRegex(String arg) {
|
||||
if (arg == null) {
|
||||
return 0;
|
||||
}
|
||||
final String[] words = arg.split("[\\pP\\s&&[^']]+");
|
||||
return words.length;
|
||||
}
|
||||
|
||||
public static int countWordsUsingTokenizer(String arg) {
|
||||
if (arg == null) {
|
||||
return 0;
|
||||
}
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(arg);
|
||||
return stringTokenizer.countTokens();
|
||||
}
|
||||
|
||||
public static int countWordsManually(String arg) {
|
||||
if (arg == null) {
|
||||
return 0;
|
||||
}
|
||||
int flag = SEPARATOR;
|
||||
int count = 0;
|
||||
int stringLength = arg.length();
|
||||
int characterCounter = 0;
|
||||
|
||||
while (characterCounter < stringLength) {
|
||||
if (isAllowedInWord(arg.charAt(characterCounter)) && flag == SEPARATOR) {
|
||||
flag = WORD;
|
||||
count++;
|
||||
} else if (!isAllowedInWord(arg.charAt(characterCounter))) {
|
||||
flag = SEPARATOR;
|
||||
}
|
||||
characterCounter++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static boolean isAllowedInWord(char charAt) {
|
||||
return charAt == '\'' || Character.isLetter(charAt);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.string.equalsIgnoreCase;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringEqualsIgnoreCaseUnitTest {
|
||||
private String string1 = "equals ignore case";
|
||||
private String string2 = "EQUALS IGNORE CASE";
|
||||
|
||||
@Test
|
||||
public void givenEqualStringsWithDifferentCase_whenUsingEqualsIgnoreCase_ThenTheyAreEqual() {
|
||||
assertThat(string1.equalsIgnoreCase(string2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEqualStringsWithDifferentCase_whenUsingApacheCommonsEqualsIgnoreCase_ThenTheyAreEqual() {
|
||||
assertThat(StringUtils.equalsIgnoreCase(string1, string2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStringAndNullValue_whenUsingApacheCommonsEqualsIgnoreCase_ThenTheyAreNotEqual() {
|
||||
assertThat(StringUtils.equalsIgnoreCase(string1, null)).isFalse();
|
||||
}
|
||||
}
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.string.todouble;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.ParseException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToDoubleConversionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
|
||||
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
|
||||
Double.parseDouble(null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
|
||||
Double.parseDouble("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
|
||||
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
|
||||
Double.valueOf(null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
|
||||
Double.valueOf("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
|
||||
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse(null);
|
||||
}
|
||||
|
||||
@Test(expected = ParseException.class)
|
||||
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse("&");
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.string.wordcount;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class WordCountUnitTest {
|
||||
private String string1 = "This is a test sentence with eight words";
|
||||
private String string2 = "This#is%a test sentence with eight words";
|
||||
|
||||
@Test
|
||||
public void givenStringWith8Words_whenUsingRegexCount_ThenResultEqual8() {
|
||||
assertEquals(8, WordCounter.countWordsUsingRegex(string2));
|
||||
assertEquals(9, WordCounter.countWordsUsingRegex("no&one#should%ever-write-like,this;but:well"));
|
||||
assertEquals(7, WordCounter.countWordsUsingRegex("the farmer's wife--she was from Albuquerque"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWith8Words_whenUsingManualMethod_ThenWordCountEqual8() {
|
||||
assertEquals(8, WordCounter.countWordsManually(string1));
|
||||
assertEquals(9, WordCounter.countWordsManually("no&one#should%ever-write-like,this but well"));
|
||||
assertEquals(7, WordCounter.countWordsManually("the farmer's wife--she was from Albuquerque"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStringWith8Words_whenUsingTokenizer_ThenWordCountEqual8() {
|
||||
assertEquals(8, WordCounter.countWordsUsingTokenizer(string1));
|
||||
assertEquals(3, new StringTokenizer("three blind mice").countTokens());
|
||||
assertEquals(4, new StringTokenizer("see\thow\tthey\trun").countTokens());
|
||||
assertEquals(7, new StringTokenizer("the farmer's wife--she was from Albuquerque", " -").countTokens());
|
||||
assertEquals(10, new StringTokenizer("did,you,ever,see,such,a,sight,in,your,life", ",").countTokens());
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
Reference in New Issue
Block a user