Results 1 to 8 of 8
  1. #1
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default how add parse in scanner

    how add parse in scanner
    this is code take if statement and split to types
    and i want to add to code parse
    where check if statement that is in code with if correct statement and know do it is correct or no ?
    plz anyone help me and thank you

    Java 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 i;
        double d;
        boolean b;
        String str;
        String str1;
        String str2;
        String str3;
        String str4;
        String str5;
    
        FileWriter fout = new FileWriter("test.txt");
        fout.write("if int a = 30 int c = a / 5 ;");
        fout.close();
    
        FileReader fin = new FileReader("Test.txt");
    
        Scanner src = new Scanner(fin);
    
        while (src.hasNext()) {
          if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
          } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
          } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
          } else  if (src.hasNext("if")){
            str = src.next("if");
            System.out.println("Resrved words: " + str);
          }
          else  if (src.hasNext("=")){
            str1 = src.next("=");
            System.out.println("Compute: " + str1);
          }
          else  if (src.hasNext(";")){
            str2 = src.next(";");
            System.out.println("Compute: " + str2);
          }
          else  if (src.hasNext("/")){
            str2 = src.next("/");
            System.out.println("Compute: " + str2);
          }
          /*else  if (src.hasNext("(")){
            str3 = src.next("(");
            System.out.println("Compute: " + str3);
          }
          else  if (src.hasNext(")")){
            str4 = src.next(")");
            System.out.println("Compute: " + str4);
          }*/
          else  if (src.hasNext("==")){
            str5 = src.next("==");
            System.out.println("Logical: " + str5);
          }
          else  if (src.hasNext("==")){
            str5 = src.next("==");
            System.out.println("Logical: " + str5);
          }
          else {
              str = src.next();
            System.out.println("Variables: " + str);
          }
           
        }
        fin.close();
      }
    }

  2. #2
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    162
    Rep Power
    5

    Default src.findInLine("if")

    hi
    you can use
    src.findInLine("if")
    to get if
    src.findInLine("int")
    to get int etc.....

    please refer
    Scanner (Java 2 Platform SE 5.0)

  3. #3
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    thank you for helping
    but i want to check on "if" in first then "int" then "a" or any word except a ...etc
    thnks

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Also at bytes.com

  5. #5
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Also at bytes.com
    OK where problem in this?
    there is anyone reply me

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by asabdo View Post
    OK where problem in this?
    Yes, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

    there is anyone reply me
    Hopefully, but again being upfront about the cross-posting will help increase your odds.

    Edit: Also if possible, please edit your questions. For some reason, they seem difficult for me to understand. I assume that English is not your native language. Am I correct?

    Much luck.
    Last edited by Fubarable; 01-15-2010 at 06:07 AM.

  7. #7
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yes, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.


    Hopefully, but again being upfront about the cross-posting will help increase your odds.

    Much luck.
    Thanks on information

  8. #8
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

Similar Threads

  1. using parse
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 01-15-2010, 02:32 AM
  2. Use Scanner to parse text file, adding to HashMap
    By JordashTalon in forum New To Java
    Replies: 0
    Last Post: 03-05-2009, 12:08 AM
  3. SAX Parse
    By bluefloyd8 in forum New To Java
    Replies: 1
    Last Post: 01-25-2008, 04:57 PM
  4. How to parse in html
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 01:29 AM
  5. Parse error
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •