Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example.
Scanner input = new Scanner(file);
while(input.hasNext()) {
String word = input.next();
}
written by objects
\\ tags: file, parse, Scanner, text, word
The URL class can be used to parse a URL and retrieve the components such as host name
String urlstring = "http://www.objects.com.au/services/sherpa.html"
URL url = new URL(urlstring};
String host = url.getHost(); // returns "www.objects.com.au"
written by objects
\\ tags: host, parse, url
The BigDecimal constructors do not take the Locale into account when parsing number strings.
This means the following code will throw a NumberFormatException
Locale.setDefault(new Locale("nl", "NL"));
String s = "2.343.298,09324798";
BigDecimal bd = new BigDecimal(s);
To parse localized strings as BigDecimal we instead need to use the DecimalFormat class
Locale.setDefault(new Locale("nl", "NL"));
String s = "2.343.298,09324798";
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
df.setParseBigDecimal(true);
BigDecimal bd = (BigDecimal) df.parse(s);
written by objects
\\ tags: BigDecimal, DecimalFormat, Locale, NumberFormat, parse