0
class sortMerge
{
static void sort(String a[])
{
int l,i,j;
l=a.length;
char x,n;
String p,t="",q;
for(i=0;i<l;i++)
{
p=a[i];
for(j=0;j<l;j++)
{
q=a[j+1];
x=p.charAt(i);
n=q.charAt(j);
if(x>n)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
public static void main(String args[])throws IOException
{
int i,j,k;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no. of names for the first array");
int a=Integer.parseInt(in.readLine());
String c[]=new String[a];
System.out.println("Enter "+a+" names");
for(i=0;i<a;i++)
c[i]=in.readLine();
System.out.println("Enter the no. of names for the second array");
int b=Integer.parseInt(in.readLine());
String d[]=new String[b];
System.out.println("Enter"+b+"names");
for(i=0;i<b;i++)
d[i]=in.readLine();
String e[]=new String[a+b];
for(j=0;j<a;j++)
e[j]=c[j];
for(k=0;k<b;k++)
e[j+1]=d[k];
sort(e);
System.out.println("Merged Sorted Array:");
for(i=0;i<e.length;i++)
System.out.println(e[i]);
sort(c);
System.out.println("Sorted First Array");
for(i=0;i<c.length;i++)
System.out.println(c[i]);
sort(d);
System.out.println("Sorted Second Array");
for(i=0;i<d.length;i++)
System.out.println(d[i]);
}
}
5 answers
1
import java.io.*;
class sortMerge
{
static void sort(String a[])
{
int l,i,j;
l=a.length;
char x,n;
String p,t="",q;
for(i=0;i<l;i++)
{
// p=a[i];
for(j=i+1;j<l;j++)
{
//YOU WERE TRYING TO COMPARE ONLY THE FIRST CHARACTERS
//WHAT IF FIRST CHARACTERS ARE SAME
//TO COMPARE TWO STRINGS SIMPLY USE compareTo()
// q=a[j+1];
// x=p.charAt(i);
// n=q.charAt(j);
// if(x>n)
if(a[i].compareTo(a[j])>0)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
public static void main(String args[])throws IOException
{
int i,j,k;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no. of names for the first array");
int a=Integer.parseInt(in.readLine());
String c[]=new String[a];
System.out.println("Enter "+a+" names");
for(i=0;i<a;i++)
c[i]=in.readLine();
System.out.println("Enter the no. of names for the second array");
int b=Integer.parseInt(in.readLine());
String d[]=new String[b];
System.out.println("Enter"+b+"names");
for(i=0;i<b;i++)
d[i]=in.readLine();
String e[]=new String[a+b];
for(j=0;j<a;j++)
e[j]=c[j];
for(k=0;k<b;k++)
// e[j+1]=d[k];
e[j+k] = d[k];
sort(e);
System.out.println("Merged Sorted Array:");
for(i=0;i<e.length;i++)
System.out.println(e[i]);
sort(c);
System.out.println("Sorted First Array");
for(i=0;i<c.length;i++)
System.out.println(c[i]);
sort(d);
System.out.println("Sorted Second Array");
for(i=0;i<d.length;i++)
System.out.println(d[i]);
}//end of main
}//end of class
0
1
In these lines,
q=a[j+1];
x=p.charAt(i);
n=q.charAt(j);
you stored a single character in string 'q' and then you were trying to access jth character from it which doesn't exist. Hence you got NullPointerException.
Hope that makes it clear.
0
0
Oh yes! Sorry I didn't notice it. Still it is incorrect since the string 'q' might not have an index pointed by the variable 'j'.
Log in to your account to answer this questions.