public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6.0 true ";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// print a line of the scanner
System.out.println("" + scanner.nextLine());
// change the locale of the scanner
scanner.useLocale(Locale.ENGLISH);
// display the new locale
System.out.println("" + scanner.locale());
// close the scanner
scanner.close();
}
}
public class ScannerUseLocaleDemo {
public static void main(String[] args) {
// Initialize Scanner object
Scanner scan = new Scanner("William Hectre/Male/24");
// initialize the string delimiter
scan.useDelimiter("/");
// Printing the delimiter used
System.out.println("The delimiter used is "+scan.delimiter());
// set the locale to be used
Locale locale = Locale.ENGLISH;
// set the locale to the scanner object
scan.useLocale(locale);
// Printing the tokenized Strings
while(scan.hasNext()){
System.out.println(scan.next());
}
// closing the scanner stream
scan.close();
}
}
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Geeksforgeeks has Scanner Class Methods";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// print a line of the scanner
System.out.println("Scanner String: \n"
+ scanner.nextLine());
// display the previous locale
System.out.println("Current Lcoale: "
+ scanner.locale());
// change the locale of the scanner
scanner.useLocale(Locale.ENGLISH);
System.out.println("Changing Locale to ENGLISH");
// display the new locale
System.out.println("Updated Locale: "
+ scanner.locale());
// close the scanner
scanner.close();
}
}
public class ScannerUncommonUses {
@SuppressWarnings("resource")
public static void main(String[] args) {
// Parsing primitive numbers
boolean bl = new Scanner("false").nextBoolean();
byte b = new Scanner("16").nextByte();
int n = new Scanner("42").nextInt();
long l = new Scanner("99999999999").nextLong();
float f = new Scanner("4.2").nextFloat();
double d = new Scanner("99.99999999").nextDouble();
System.out.printf("/***** Parsing primitive numbers *****/\n\n");
System.out.printf("boolean: %b\n", bl);
System.out.printf("byte: %d\n", b);
System.out.printf("int: %d\n", n);
System.out.printf("long: %d\n", l);
System.out.printf("float: %f\n", f);
System.out.printf("double: %f\n\n", d);
// Using Locale to parse numbers in specific languages
try {
double doubleLocale = new Scanner("1.234,56").useLocale(
Locale.ITALIAN).nextDouble();
System.out.printf("/***** Using Locales to parse numbers in specific languages *****/\n\n");
System.out.printf("double (Locale = Italy): %f", doubleLocale);
} catch (InputMismatchException ime) {
ime.printStackTrace(System.err);
}
}
}
public class Main {
public static void main(String[] args) {
String s = "java2s.com 1 + 1 = 2.0 true ";
Scanner scanner = new Scanner(s);
System.out.println(scanner.nextLine());
// change the locale of the scanner
scanner.useLocale(Locale.ENGLISH);
// display the new locale
System.out.println(scanner.locale());
scanner.close();
}
}
public In(Socket socket) {
if (socket == null) throw new NullPointerException("argument is null");
try {
InputStream is = socket.getInputStream();
scanner = new Scanner(new BufferedInputStream(is), CHARSET_NAME);
scanner.useLocale(LOCALE);
}
catch (IOException ioe) {
throw new IllegalArgumentException("Could not open " + socket);
}
}
public In(URL url) {
if (url == null) throw new NullPointerException("argument is null");
try {
URLConnection site = url.openConnection();
InputStream is = site.getInputStream();
scanner = new Scanner(new BufferedInputStream(is), CHARSET_NAME);
scanner.useLocale(LOCALE);
}
catch (IOException ioe) {
throw new IllegalArgumentException("Could not open " + url);
}
}
public In(File file) {
if (file == null) throw new NullPointerException("argument is null");
try {
// for consistency with StdIn, wrap with BufferedInputStream instead of use
// file as argument to Scanner
FileInputStream fis = new FileInputStream(file);
scanner = new Scanner(new BufferedInputStream(fis), CHARSET_NAME);
scanner.useLocale(LOCALE);
}
catch (IOException ioe) {
throw new IllegalArgumentException("Could not open " + file);
}
}
public Memory useLocale(Environment env, Memory... args) {
scanner.useLocale(args[0].toObject(WrapLocale.class).getLocale());
return new ObjectMemory(this);
}
public static List<ProcessInformations> buildProcessInformations(InputStream in,
boolean windows, boolean macOrAix) {
final String charset;
if (windows) {
charset = "Cp1252";
} else {
charset = "UTF-8";
}
final Scanner sc = new Scanner(in, charset);
sc.useRadix(10);
sc.useLocale(Locale.US);
sc.nextLine();
if (windows) {
sc.nextLine();
sc.nextLine();
}
final List<ProcessInformations> processInfos = new ArrayList<ProcessInformations>();
while (sc.hasNext()) {
final ProcessInformations processInfo = new ProcessInformations(sc, windows, macOrAix);
processInfos.add(processInfo);
}
return Collections.unmodifiableList(processInfos);
}