Chauhan's blog

By Chauhan, history, 8 years ago, In English

I am a learner just now.And I am trying problem "609A" in java. there is an error "_Runtime error:exit code is 1_" at the time of submitting the code. Please help me with this code.

and code is -:

//609A-USB selection for given memory size//

import java.util.Scanner;

class data
{
    
    int n,s,m=0,i;
    int[] a = new int[n];
    void data()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("\n");
        //System.out.println("enter number of drives and size of file :");
        n = in.nextInt();
        s = in.nextInt();
        //System.out.println("n & s ="+n+"   "+s);
        int[] b = new int[n];
        for (i=0;i<n;i++)
        {
            b[i] = in.nextInt();  
        }
        a = b;
    }
}
public class usb
{
    public static void main()
    {
        int c=0,temp = 0,sum = 0;
        data da = new data();
        da.data();
        for(int i= 0; i<da.n;i++)               //0 to length of a.
        {
            for(int j=i; j<da.n;j++)            //from i to length.a;
            {
                if(da.a[i]<da.a[j])
                {
                    da.a[i] = da.a[i]+da.a[j];
                    da.a[j] = da.a[i]-da.a[j];
                    da.a[i] = da.a[i]-da.a[j];
                }
            }
        }
        int f=0;
        for(int i= 0; i<da.n;i++)
        {
            if(f==0)
            {
                sum=0;
                c=0;
            }
            for(int j=i;j<da.n;j++)
            {
            temp = da.a[j];
            sum = sum + temp;
            if(temp == da.s)
            {
                c=1;
                f=1;
                break;
            }else
            if(f==0)
            {
                if(sum == da.s)
                {
                    c++;
                    f=1;
                    break;
                }else
                 if(sum < da.s)
                 {
                     c++;
                 }else
                if(sum > da.s)
                {
                    sum = sum - temp;
                }
            }
            }
        }
        if(sum<da.s)
        c=0;
        if (c>0)
        System.out.println(c);
        else
        System.out.println("not sufficient memory");
    }
}
            
  • Vote: I like it
  • -12
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Well You should use ideone to format your code, and put a link to the problem, if you expect people to help you.

Anyways, I think that a public class should be the first thing in your code. Rest shall be below or nested inside. Try that.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey thanks for your help. But now there is an error i.e. "runtime error : exit code is 1". Can you help me in this please. This code is doing good on my local machine.

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      How do you run it? Correct signature for main is public static void main(String[] args) (even if you don't use args), not just main without arguments.