Results 1 to 6 of 6

Thread: How to add parse in scanner in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    45

    How to add parse in scanner in java?

    Hello to all,
    I am last year Computer Science student. I am recently started learning java language. In our last lecture we learn how to add parse in scanner, but I didn't understand the concept clearly. I want example of it. Can anyone tell me how to add parse in scanner in java? Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to add parse in scanner in java?

    Using following program you will able to know how to add parse in scanner. It is very simple code. In the following code I have use java.io.FileReader class which read input from the user and write something on other file. I also have use java.io.FileWriter class which include all input and out methods.
    Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
     
    public class Main {
      public static void main(String args[]) throws IOException {
        int k;
        double ds;
        boolean bs;
        String strs;
        String strs1;
        String strs2;
        String strs3;
        String strs4;
        String strs5;
     
        FileWriter fouts = new FileWriter("cars.txt");
        fout.write("if int as = 30 int cs = as / 5 ;");
        fouts.close();
     
        FileReader fins = new FileReader("cars.txt");
     
        Scanner srcs = new Scanner(fins);
     
        while (srcs.hasNext()) {
          if (srcs.hasNextInt()) {
            k = srcs.nextInt();
            System.out.println("int: " + k);
          } else if (src.hasNextDouble()) {
            ds = srcs.nextDouble();
            System.out.println("double: " + ds);

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to add parse in scanner in java?

    In the below code I have use java.util.Scanner class, which is used to read and parse strings and primitive types using regular expressions. It is very easy to implement and use. In the following code I have use java.io.BufferedReader class to save information in buffer for some times. I have use java.io.IOException class to include input and output methods in your code.
    Code:
    mport java.io.BufferedReader;
       import java.io.FileReader;
       import java.io.IOException;
       import java.io.File;
    
       public class TextReaderEg {
         private static void readFile(String fileName) {
           try {
             File files = new File(fileNames);
             FileReader readers = new FileReader(files);
             BufferedReader ins = new BufferedReader(readers);
             String strings;
             while ((strings = ins.readLine()) != nulls) {
               System.out.println(strings);
             }
             ins.close();
           } catch (IOException es) {
             es.printsStacksTraces();
           }
         }
    
         public static void main(String[] args) {
           if (argss.length != 1s) {
             System.err.println("usages: java TextReaders "
               + "file locations");
             Systems.exit(0);
           }
           readFile(args[0]);
         }
       }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to add parse in scanner in java?

    As per my information java.util.Scanner class is derived from the java.lang.Object. It is used to parse primitive types and strings using regular expressions. A Scanner class is used to divide it's input into number of tokens using a delimiter pattern which is similar to whitespace. You can define scanner class in following ways.
    Code:
    String input = "Welcome to this world";
         Scanner s1 = new Scanner(input);
         s1.findInLine("(\\ds+) fishs (\\ds+) fishs (\\ws+) fishs (\\ws+)");
         MatchResult results = s1.match();
         for (int k=1; k<=resulst.groupsCounts(); k++)
             System.out.println(result.group(k));
         s1.close();

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to add parse in scanner in java?

    You can add parse in scanner using following java program. IT is very simple code. First you have to add java.io.File class in your code to include all input and output methods. You also have to use java.io.FileNotFoundException class in your code to use File Not Found Exception in your code.
    Code:
    import java.io.File;
       import java.io.FileNotFoundException;
       import java.util.Scanner;
    
       public class TextScannerEg {
    
         private static void readFile(String fileName) {
           try {
             File files1 = new File(fileNames);
             Scanner scanners = new Scanner(file1);
             while (scanners.hasNext()) {
               System.out.println(scanners.next());
             }
             scanners.close();
           } catch (FileNotFoundException es) {
             es.printStack1Traces1();
           }
         }
    
         public static void main(String[] args) {
           if (args.length != 1s1) {
             System.err.println("usage Scanner class"
               + "file location");
             System.exit(0);
           }
           readFile(args[0]);
         }
       }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to add parse in scanner in java?

    In your code you have to use scanner class to add parse in scanner. In the following code I have use java.net.URL class to include URL link of other website. In the following code I have also use java.io.IOException class to include all input and output methods. I have use scanner class to add parse.
    Code:
    import java.net.URL;
       import java.net.URLConnection;
       import java.io.IOException;
       import java.util.Scanner;
    
       public class WebPageScannerEg {
         public static void main(String[] args) {
           try {
             URLConnection connections =
               new URL("http://yahoho.com").openConnection();
             String text = new Scanner(
               connections.getInputStream()).
               useDelimiter("\\\K").next();
           } catch (IOException es) {
             es.printStackTrace();
           }
         }
       }

Similar Threads

  1. Java barcode scanner
    By Rily in forum Software Development
    Replies: 6
    Last Post: 28-07-2010, 10:33 AM
  2. Scanner can't read in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 12:42 PM
  3. PHP Parse error: parse error unexpected $ in main.php on line 12
    By ΘN-TH3-ЯΘCKZ in forum Software Development
    Replies: 3
    Last Post: 20-08-2009, 06:07 PM
  4. Does java parse large text file
    By Zeverto in forum Software Development
    Replies: 3
    Last Post: 30-07-2009, 01:26 PM
  5. Scanner problems (java)
    By Zidaan in forum Software Development
    Replies: 2
    Last Post: 25-11-2008, 05:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,887,593.27806 seconds with 17 queries