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");
}
}
}
我编写了这段代码,以确定给定的语法是否是递归的。当我编译程序时,它会在行中为NullPointerException
提供
if(left[i].charAt(0)==right[j].charAt(0))
和
isleft(left,right);
如何删除例外?
答案 0 :(得分:2)
我猜测你输入的问题,你只是将字符串数组长度设为10。
String[] left=new String[10];
String[] right=new String[10];
不要HardCode字符串数组长度
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();
}
可能,这将是问题
答案 1 :(得分: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
如果你得到了没有。从控制台生成prod,String数组需要相应地设置,因为我所做的更改是::
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
答案 2 :(得分:0)
你不能初始化没有大小的数组。您已经将数组大小设置为10,如果输入的数据大于10或小于10,则会出现错误。因此,如果要使用动态大小,则应使用java集合。最好的方法是数组列表
static int isLeft(ArrayList left,ArrayList right)
{
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");
}
}