why should i use two swap functions in second and i only one swap function in first
//first one am getting correct answer with only one swap
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
perm(s,0,s.length());
}
static void perm(String s,int left,int right)
{
if(left==right)
{
System.out.print(s+" ");
}
for(int i=left;i<right;i++)
{
s=swap(s,i,left,1);
perm(s,left+1,right);
}
}
static String swap(String s,int i,int j)
{
char ch[]=s.toCharArray();
char temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
s=new String(ch);
return s;
}
}
//in the second code am getting correct answer with two swap functions
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int nums[]={1,2,3};
perm(nums,0,nums.length);
}
static void perm(int[] nums,int left,int right)
{
for(int k=0;k<nums.length;k++)
{
System.out.print(nums[k]);
}
System.out.print(" ");
if(left==right)
{
ArrayList<Integer> l1=new ArrayList<Integer>();
for(int i=0;i<nums.length;i++)
{
//l1.add(nums[i]);
}
//l.add(l1);
}
for(int i=left;i<right;i++)
{
nums=swap(nums,i,left);
perm(nums,left+1,right);
nums=swap(nums,i,left);
}
}
static int[] swap(int[] nums,int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
for(int k=0;k<nums.length;k++)
{
nums[k]=nums[k];
}
return nums;
}
}
Related
This is the program for print the character of given index.But is gets error in function.
I give the function return type as char.But compiler told to rename the return type of main function
import java.util.Scanner;
class Demo5{
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
public char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}
}
}
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
}
public static char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}
I was Supposed to get the output as
{-10,6,7,13,20,80,90,100}
but I am getting the output as
{20,100,13,6,7,80,-10,90}
in SelectionSort. Why am I getting this output? How can I solve this problem?
public class x{
public static void main(String args[]){
int[] a= {20,13,6,7,80,-10,90,100};
int i,j,c,largest;
for(i=a.length-1;i>0;i--){
largest=0;
for(j=1;j<=(a.length-1);j++){
if(a[j]>a[largest]){
largest=j;
}
}
swap(a,largest,i);
}
for(c=0;c<a.length;c++)
System.out.println(a[c]);
}
public static void swap(int[] arr, int a, int b){
int temp;
if(a==b){
return;
}
temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}
public class x{
public static void main(String args[]){
int[] a= {20,13,6,7,80,-10,90,100};
int i,j,c,largest;
for(i=a.length-1;i>=0;i--){
largest=0;
for(j=1;j<=i;j++){
if(a[j]>a[largest]){
largest=j;
}
}
swap(a,largest,i);
}
for(c=0;c<a.length;c++)
System.out.println(a[c]);
}
public static void swap(int[] arr, int a, int b){
int temp;
if(a==b){
return;
}
temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}
package individual;
import java.io.*;
import java.util.*;
public class Individual{
public static void getContent(Scanner inp,String[] contents){
int i;
for(i=0;i<contents.length;i++)
contents[i]=inp.next();
public static void Sort(String[] contents){
int i,j;
int min;
String temp;
//for selection sort
for(i=0;i<contents.length-1;i++)
{
min=i;
for(j=i+1;j<contents.length;j++)
if(contents[j].compareTo(contents[min])<0)
min=j;
temp=contents[i];
contents[i]=contents[min];
contents[min]=temp;
}
}
public static void main(String[] args) throws FileNotFoundException{
String[] newAyat=new String[50];
//read From file that i save in drive
Scanner inFile=new Scanner(new FileReader("D:\\newFile.txt"));
getContent(inFile,newAyat);
Sort(newAyat);
}
}
}
so when i run the program i got an error. The output is not what i want. Is there any problems with my codes. And how to print out the sorting results?
This is the question that I try to solve:
Based on the list relates , you must program can display a list of words beginning with ' a ' , ' b ' , ' c ' and so on .
package textconcordance;
import java.util.*;
import java.io.*;
public class TextConcordance {
final static int SEN_LONG=8;
public static void getAyat(Scanner inp,String[] Sentence){
int i;
while(inp.hasNext()){
for(i=0;i<Sentence.length;i++)
Sentence[i]=inp.next();
}
}
//sort sentence
public static void sortSen(String[] Sentence){
int i,j;
int min;
String temp;
for(i=0;i<Sentence.length-1;i++)
{
min=i;
for(j=i+1;j<Sentence.length;j++)
if(Sentence[j].compareTo(Sentence[min])<0)
min=j;
temp=Sentence[i];
Sentence[i]=Sentence[min];
Sentence[min]=temp;
}
}
//for print
public static void print(String[] Sentence){
for(int i=0;i<Sentence.length;i++)
System.out.print(Sentence[i]+",");
}
public static void main(String[] args) throws FileNotFoundException{
String[] Sentence=new String[SEN_LONG];
Scanner inFile=new Scanner(new FileReader("D://Ghost.txt"));
getAyat(inFile,Sentence);
sortSen(Sentence);
inFile=null;
inFile=new Scanner(new FileReader("D://Story.txt"));
print(Sentence);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
the program is to count the frequency of each word in a sentence.
This is the source code:
import java.io.*;
import java.util.Scanner;
class Dictionary
{
String key;
int val;
public Dictionary()
{
key="";
val=0;
}
void insert(String k)
{
key=k;
val=1;
System.out.println("\nInserted key with value: "+k);
}
void update()
{
val++;
}
String retkey()
{
return key;
}
int retval()
{
return val;
}
public void display()
{
System.out.println("Word = "+key);
System.out.println("Number = "+val);
}
}
class stack
{
Dictionary D[]=new Dictionary[50];
int n;
int top;
public stack(int N)
{
n=N;
top=-1;
// D=new Dictionary[n];
}
public void push(String p)
{
top++;
System.out.println("pushing "+p+"\n top=="+top);
D[top].insert(p);
System.out.println("\ntop="+top);
}
public void check(String p)
{
int i=0,flag=1;
String q="";
if(top==-1)
{
System.out.println("top=-1");
push(p);
flag=0;
}
else
{
for(i=0;i<=top;i++)
{
q=D[i].retkey();
if(q.equals(p))
{
D[i].update();
flag=0;
}
else
flag=1;
}
if(flag==1)
push(p);
}
}
public void print()
{
D[top].display();
}
}
public class Wrdcnt
{
public static void main(String[] args)throws Exception
{
// Dictionary D[]=new Dictionary() ;
String st="";
String p="";
System.out.println("Enter String: ");
Scanner in= new Scanner(System.in);
st=in.nextLine();
/* no of words*/
int n=count(st);
int j=0;
System.out.println("No of words: "+n);
stack stck =new stack(n);
for(int i=0;i<=st.length();i++)
{
if(i==st.length())
{
p = st.substring(j,i);
System.out.println(""+p);
stck.check(p);
System.out.println("Checking done for word: "+p);
j=0;
break;
}
if(st.charAt(i)==' ')
{
p = st.substring(j,i);
System.out.println(""+p);
stck.check(p);
j=i+1;
System.out.println("Checking done for word: "+p);
}
}
}
public static int count(String s)
{
int w=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' ')
{
w++;
}
}
return (w+1);
}
}
output:
Enter String:
Roses are red
No of words: 3
Roses
top=-1
pushing Roses
top==0
Exception in thread "main" java.lang.NullPointerException
at stack.push(Wrdcnt.java:51)
at stack.check(Wrdcnt.java:61)
at Wrdcnt.main(Wrdcnt.java:117)
In this line:
D[top].insert(p);
You access the variable D[top], which happens to be null.
When trying to invoke insert(p) on null, you get a NullPointerException.
This happens because you only initialize the array, but don't initialize the objects themselves to populate the array:
Dictionary D[]=new Dictionary[50];
Is allocating place for the Dictionary[] array, but NOT allocating the Dictionary objects themselves.
I was debugging my code and found a problem with my Selection Sort Algorithm.
The code below almost sorts it, but I cant understand why all of it is not sorted.
have tried everything but to no avail.
import java.util.Random;
public class Help
{
//private static int[] myarray=new int[20];
private static int[] array;
public static void main(String[] args)
{
array=new int[20];
fillArrayRandom(array);
sortAscending(array);
///This is the bit that does not do what it is meant to do!
for(int i=0;i<array.length;i++)
{
// System.out.printf("Testing %d%n",myarray[i]);
}
}
public static void fillArrayRandom(int[] array)
{
int i;
for(i=0;i<array.length;i++)
{
array[i]=getRandomNum();
}
}
public static int getRandomNum()``
{
Random num=new Random();
int TestNumber=num.nextInt(2000);
return TestNumber;
}
public static void sortAscending(int[] array)
{
int smallest;
for(int i=0;i<array.length-1;i++)
{
smallest=i;
for(int index=i+1;index<array.length;index++)
{
if(array[index]<array[smallest])
smallest =index;
swap(i,smallest);
}
System.out.printf("%d%n",array[i]);
}
}
public static void swap(int first,int second)
{
int temporary=array[first];
array[first]=array[second];
array[second]=temporary;
}
}
You need to swap after the inner loop has completed:
public static void sortAscending(int[] array)
{
int smallest;
for(int i=0;i<array.length-1;i++)
{
smallest=i;
for(int index=i+1;index<array.length;index++)
{
if(array[index]<array[smallest])
smallest =index;
}
if (i != smallest) swap(i,smallest);
System.out.printf("%d%n",array[i]);
}
}