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 delimiter of this scanner
scanner.useDelimiter("Wor");
// display the new delimiter
System.out.println("" + scanner.delimiter());
// close the scanner
scanner.close();
}
}
public class ScannerUseDelimiterDemo {
public static void main(String[] args) {
// Initialize Scanner object
Scanner scan = new Scanner("Anna Mills/Female/18");
// initialize the string delimiter
scan.useDelimiter("/");
// Printing the delimiter used
System.out.println("The delimiter use is "+scan.delimiter());
// 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 old delimiter
System.out.println("Old delimiter: "
+ scanner.delimiter());
// change the delimiter of this scanner
scanner.useDelimiter(Pattern.compile(".ll."));
// display the new delimiter
System.out.println("New delimiter: "
+ scanner.delimiter());
// close the scanner
scanner.close();
}
}
public class ScannerUseDelimiterExample1 {
public static void main(String args[]){
String str = "JavaTpoint! 13 + 13.0 = 26.0 false ";
//Create scanner with the specified String Object
Scanner scanner = new Scanner(str);
//Print String
System.out.println("String: " + scanner.nextLine());
//Change the delimiter of this scanner
scanner.useDelimiter("vaT");
//Display the new delimiter
System.out.println("New delimiter: " +scanner.delimiter());
scanner.close();
}
}
public class MainClass {
public static void main(String args[]) throws IOException {
FileWriter fout = new FileWriter("test.txt");
fout.write("2, 3.4, 5,6, 7.4, 9.1, 10.5, done");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
// Set delimiters to space and comma.
// ", *" tells Scanner to match a comma and zero or more spaces as
// delimiters.
src.useDelimiter(", *");
// Read and sum numbers.
while (src.hasNext()) {
if (src.hasNextDouble()) {
System.out.println(src.nextDouble());
} else {
break;
}
}
fin.close();
}
}
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
String response = null;
if (hasInput) {
response = scanner.next();
}
scanner.close();
return response;
} finally {
urlConnection.disconnect();
}
}
protected void processLine(String line) {
if (!line.contains("=") || line.isEmpty()) {
return;
}
//use a second Scanner to parse the content of each line
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("=");
if (lineScanner.hasNext()) {
String key = lineScanner.next();
String value = "";
if (lineScanner.hasNext()) {
value = lineScanner.next();
}
map.put(key, value);
}
//no need to call lineScanner.close(), since the source is a String
}
public static void parse (
final List <NameValuePair> parameters,
final Scanner scanner,
final String charset) {
scanner.useDelimiter(PARAMETER_SEPARATOR);
while (scanner.hasNext()) {
String name = null;
String value = null;
String token = scanner.next();
int i = token.indexOf(NAME_VALUE_SEPARATOR);
if (i != -1) {
name = decodeFormFields(token.substring(0, i).trim(), charset);
value = decodeFormFields(token.substring(i + 1).trim(), charset);
} else {
name = decodeFormFields(token.trim(), charset);
}
parameters.add(new BasicNameValuePair(name, value));
}
}
public void read(Integer[][] array) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("signal.CSV"));
int columns = 0;
int row = 0;
scanner.useDelimiter("\\D+");
while (scanner.hasNext()) {
array[row][columns] = scanner.nextInt();
columns += 1;
if (columns == 4) {
columns = 0;
row += 1;
}
//System.out.println(array[0][0]);
}
scanner.close();
}
public void readFromFile() throws IOException
{
Scanner in = new Scanner(new BufferedReader(new FileReader(file)));
try
{
in.useDelimiter(",");
headerLine = in.nextLine(); // Skip header row
while (in.hasNextLine())
{
String srgName = in.next();
String mcpName = in.next();
String side = in.nextLine().substring(1);
srgParamName2ParamCsvData.put(srgName, new ParamCsvData(srgName, mcpName, Integer.valueOf(side)));
}
}
finally
{
in.close();
}
}