2014-01-17 66 views
-2
public class leftrec { 

static int isleft(String[] left,String[] right) 

    { 
     int f=0; 
     for(int i=0;i<left.length;i++) 
     { 
      for(int j=0;j<right.length;j++) 

      { 
       if(left[i].charAt(0)==right[j].charAt(0)) 
       { 
        System.out.println("Grammar is left recursive"); 
        f=1; 
       } 

      } 
     } 
      return f; 
    } 
    public static void main(String[] args) { 
     // TODO code application logic here 
     String[] left=new String[10]; 
     String[] right=new String[10]; 
     Scanner sc=new Scanner(System.in); 
     System.out.println("enter no of prod"); 
     int n=sc.nextInt(); 
     for(int i=0;i<n;i++) 
     { 
      System.out.println("enter left prod"); 
      left[i]=sc.next(); 
      System.out.println("enter right prod"); 
      right[i]=sc.next(); 
     } 

     System.out.println("the productions are"); 
     for(int i=0;i<n;i++) 
     { 
      System.out.println(left[i]+"->"+right[i]); 
     } 
     int flag=0; 
     flag=isleft(left,right); 
      if(flag==1) 
      { 
       System.out.println("Removing left recursion"); 
      } 
      else 
      { 
       System.out.println("No left recursion"); 
      }  
    } 
} 

我寫過這段代碼,看看給定的語法是否是遞歸的。當我編譯程序它給了我行刪除Java中的NullPointerException

if(left[i].charAt(0)==right[j].charAt(0)) 

isleft(left,right); 

如何刪除異常NullPointerException

+0

類名應以大寫字母開頭 – lakshman

+0

使用Java編碼約定來定義你的類。類名應該在Camel Case中。 –

+0

當你編譯代碼?或者運行代碼? –

回答

2

我想與你輸入的問題,你只是把字符串數組長度爲10

String[] left=new String[10]; 
String[] right=new String[10]; 

不要硬編碼字符串數組長度

int n=sc.nextInt(); 
String[] left=new String[n]; 
String[] right=new String[n]; 
for(int i=0;i<n;i++){ 
    System.out.println("enter left prod"); 
    left[i]=sc.next(); 
    System.out.println("enter right prod"); 
    right[i]=sc.next(); 
} 

也許,這將是問題

+0

測試了這一點,你是對的 –

0

您需要更改代碼如下::

package com.cgi.ie2.common; 

import java.util.Scanner; 

public class LeftRecursive { 

static int isleft(String[] left, String[] right) 
{ 
    int f = 0; 
    for (int i = 0; i < left.length; i++) { 
     for (int j = 0; j < right.length; j++) 
     { 
      if (left[i].charAt(0) == right[j].charAt(0)) { 
       System.out.println("Grammar is left recursive"); 
       f = 1; 
      } 
     } 
    } 
    return f; 
} 

public static void main(String[] args) { 
    // TODO code application logic here 
    Scanner sc = new Scanner(System.in); 
    System.out.println("enter no of prod"); 
    int n = sc.nextInt(); 
    //Changes done here:::: 
    String[] left = new String[n]; 
    String[] right = new String[n]; 
    for (int i = 0; i < n; i++) { 
     System.out.println("enter left prod"); 
     left[i] = sc.next(); 
     System.out.println("enter right prod"); 
     right[i] = sc.next(); 
    } 

    System.out.println("the productions are"); 
    for (int i = 0; i < n; i++) { 
     System.out.println(left[i] + "->" + right[i]); 
    } 
    int flag = 0; 
    flag = isleft(left, right); 
    if (flag == 1) { 
     System.out.println("Removing left recursion"); 
    } else { 
     System.out.println("No left recursion"); 
    } 
} 
    } 

此代碼將消除NullpointerExceptions

如果您獲得no。從控制檯PROD的,字符串數組也需要進行相應的設置,對於我所做的更改是::

System.out.println("enter no of prod"); 
    int n = sc.nextInt(); 
    //Changes done here:::: 
    String[] left = new String[n]; 
    String[] right = new String[n]; 

而且爲了更好的代碼是什麼,我可以建議你的是,你需要遵循基本的編碼約定,這使你的代碼的可讀性,只有當它運行corectly代碼是不完美的,代碼是完美的,如果編碼慣例可循,所以請到通過以下鏈接編碼已瞭解公約的基本思路::

http://www.javacodegeeks.com/2012/10/java-coding-conventions-considered-harmful.html http://java.about.com/od/javasyntax/a/nameconventions.htm

0

你不能在沒有si的情況下初始化一個數組澤。您已經將數組大小設置爲10,如果輸入的數值大於10或小於10,則會出現錯誤。因此,如果你想使用動態大小,你應該使用一個Java集合。這個最好的辦法是數組列表

靜態INT isLeft(左ArrayList中,ArrayList中右)

{ 
     int f = 0; 
     for (int i = 0; i < left.size(); i++) { 
      for (int j = 0; j < right.size(); j++) 

      { 
       if (left.get(i).charAt(0) == right.get(j).charAt(0)) { 
        System.out.println("Grammar is left recursive"); 
        f = 1; 
       } 

      } 
     } 
     return f; 

    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     ArrayList<String> left = new ArrayList<String>(); 
     ArrayList<String> right = new ArrayList<String>(); 


     Scanner sc = new Scanner(System.in); 
     System.out.println("enter no of prod"); 
     int n = sc.nextInt(); 
     for (int i = 0; i < n; i++) { 
      System.out.println("enter left prod"); 
      String leftText = sc.next(); 
      left.add(leftText); 
      System.out.println("enter right prod"); 
      String rightText = sc.next(); 
      right.add(rightText); 
     } 

     System.out.println("the productions are"); 
     for (int i = 0; i < n; i++) { 
      System.out.println(left.get(i) + "->" + right.get(i)); 
     } 
     int flag; 
     flag = isLeft(left, right); 
     if (flag == 1) { 
      System.out.println("Removing left recursion"); 
     } else { 
      System.out.println("No left recursion"); 
     } 

    }