import java.util.*;
class Array
{
    Scanner in()
    {   return new Scanner(System.in);  }

    void out(String n)
    {   System.out.print(n);    }

    int[] getarray(int n)
    {
        int a[]=new int[n];
        for(int i=0;i<a.length;i++)
        {
            out("Enter A["+i+"]: ");
            a[i]=in().nextInt();
        }
        return a;
    }

    void showarray(int[] a)
    {
        for(int c:a)
            out("Array: " +c);
        out("\n");
    }

    void sortinc(int[] a,int n)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(a[i]>a[j])
                {
                    int temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
    }

    void sortdec(int[] a,int n)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(a[i]<a[j])
                {
                    int temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
    }

    int sum(int[] a,int n)
    {
        int s=0;
        for(int i=0;i<n;i++)
            s+=a[i];
        return s;
    }

    int search(int[] a,int n)
    {
        int s;
        out("Enter your value to search: ");
        s=in().nextInt();
        for(int i=0;i<n;i++)
        {
            if(s==a[i])
                s=a[i];
            else
                out("Search not found.");
        }
        return s;
    }

    public Array()
    {
        int n;
        out("Enter Value N: ");
        n=in().nextInt();
        int[] a=new int[n];
        a=getarray(n);
        showarray(a);
    }

    public static void main(String[] args)
    {   new Array();    }
}

Dani AI

Generated

As and noted, the exact compiler/runtime messages and a clear statement of the intended behavior are the fastest way to a precise diagnosis. Meanwhile, the listing already shows a few concrete problems that commonly cause wrong results or confusing I/O behavior.

Creating a new Scanner on System.in for every read can cause input issues; prefer a single Scanner instance reused for all input and avoid closing it until program termination. The constructor allocates an array then immediately replaces it; drop the redundant allocation and use the array's own length instead of carrying an extra n parameter through most methods. The array-printing routine prints each element without separators, which makes output hard to read.

Both sort routines use full pairwise swapping across the entire index range, which is unstable and can undo earlier swaps. A reliable alternative is to call the standard library sort for primitives (Arrays.sort) or implement a well-bounded algorithm (e.g., bubble/selection) where the inner loop limits are adjusted so elements are progressively placed in final positions. For a descending result, sort ascending then reverse, or sort boxed Integers with a comparator.

The search routine mixes the search key with the return value and emits "Search not found." on every non-matching element. Use a distinct target variable, set a found flag or return an index, and print a single "not found" message only after the loop if no match was found. Add input validation (catch InputMismatchException) so non-integer input doesn't crash the program.

Troubleshooting workflow: isolate and test get-array and show-array first, then the sort on small arrays while printing before/after and on each swap, then the search routine. Posting the exact error text and the desired output will allow a line-level fix.

Recommended Answers

All 2 Replies

Please edit your post with in 30 minutes and add code tags [code] YOUR CODE HERE [/code]
Secondly some problem description or any error messages would be beneficial (do not expect everyone to try copy&paste to run your application)

site the error code, and what is the goal of the program? don't let the readers to do the compilation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.