1

I am trying to use an array, but I am not sure if that is the right way to do this.

I want the first and second integer input to compare with one another, then if there are more they compare with each other.

So here is the piece of the code.

for(int ii = 1, j = 0; j <= copySel ; ii++, j++) {     
             int x = 0;
             int[x] check;
                // Prompt as follows
                System.out.print("Enter value " + ii + ": ");
                try {
                    c = Get();
                }
                catch (InputMismatchException e) {
                    // Display the following text in the event of an invalid input
                    System.out.println("Invalid input!");
                    ii--; x--;
                } check = c; x++;    

                System.out.print(check + " "+ x);
        }

My actual format is not this bad. I need to try to find the minimum value depending on the number of integer the user has input

static int Get()
    {
        Scanner intFind = new Scanner(System.in);
        int select;
        select = intFind.nextInt();
        return select;
    }

This is Get() ^

cant I use min(x, y) continuously?

4
  • isn't this is homework? Also what is your question? Commented Nov 13, 2012 at 6:43
  • if its a homework, add a tag of it. I have also added Commented Nov 13, 2012 at 6:44
  • 1
    Someone told me not to, because they will remove that soon. Commented Nov 13, 2012 at 6:47
  • @FahimParkar The homework tag was deprecated a couple months ago. From Tag Description **This tag is OBSOLETE and is in the process of being removed. But don't remove it without looking at the question to see if it needs cleanup. Please see this question on Meta for more information. ** Commented Nov 13, 2012 at 7:23

5 Answers 5

2
int min=a[0];

for(int i=1;i<n;i++)
{
    if(a[i] < min) 
         min = a[i];
}

System.out.println("The min is "+min);
Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by this, does it go in the original for loop?
what is n here I think n is a.length
cant I use min(x, y) continuously?
1
 System.out.print("Enter value " + ii + ": ");
 int min = Get();
 int c = 0;
 for(int ii = 1, j = 0; j < copySel ; ii++, j++) { 

            // Prompt as follows
            System.out.print("Enter value " + ii + ": ");
            try {
                c = Get();
            }
            catch (InputMismatchException e) {
                // Display the following text in the event of an invalid input
                System.out.println("Invalid input!");
                ii--;
            } min = Math.min(min, c);   

            System.out.print("minimum is:"+ min);
    }

2 Comments

if you want to use ur code you can use the following, u don't have the use clone since Get() doesn't return an array of integer but it returns only 1 int
Very nice. Thanks for the help.
0

if you have a integer array you could do as below

  Integer [] arr = {5,2,3,4,5,6,7,8};
  List<Integer> list = new ArrayList<Integer>(Arrays.asList(arr));
  Collections.sort(list);
  System.out.println("Minimum "+list.get(0)); ;

Comments

0
 Scanner in = new Scanner(System.in);
 System.out.println("Enter the integers: ");
 String s = in.nextLine();
 string[] str = s.plit(" ");
 int[] a = new a[str.length];
 for(int i =0; i< str.length; i++)
 {
     a[i] = Integer.parseInt(str[i]);
 }
 //Madar's code
 int min=a[0];

for(int i=0;i<a.length;i++)
{
    min = Math.min(a[i], min);
}

System.out.println("The min is "+min);

2 Comments

cant I use min(x, y) continuously?
yes indeed you can instead of the if statement you can write min = Math.min(a[i], min);
0

I didn't get what your trying to do exactly, but here's some errors you might consider to repair. If check is an array you have to initialize it as such:

int[] check;

Does get() give you back an array of integer, if so you cannot make check = c, you have to copy or clone the content of c into check as such:

check = (int[])c.clone();

4 Comments

I have to get the minimum value
I need to try to find the minimum value depending on the number of integers the user has input
ok then the most straight forward way is to use Mandar's way,
cant I use min(x, y) continuously?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.