how add parse in scanner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asabdo
    New Member
    • Jan 2010
    • 1

    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


    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();
      }
    }
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    It looks like you are already parsing src. I don't understand what you are trying to add to your program.

    Comment

    Working...