views:

176

answers:

4

after execution of my program written in java I see this output: java result: 2147483647 . what does this number means anyway?

this is the code. it should actually run random test cases on an acm problem solution! no way to stop normally! and when I stop it manually I see this output:

Java Result: 2147483647 BUILD SUCCESSFUL (total time: 8 minutes 49 seconds)

I just want to know what does that number means?

package acm;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;
public class mamoth {
static Scanner input = new Scanner(System.in);
public static String planets;
public static int H;
public static int A;
public static Random random = new Random();

public static void main(String []args)
{
    while(!(planets = /*input.nextLine()*/Integer.toString(random.nextInt(10)+1) + " " + Integer.toString(random.nextInt(10)+1)).equals("0 0")){
        System.out.println(planets);
    //while(!(planets = input.nextLine()).equals("0 0")){
        int index;
        index = planets.indexOf(' ');
        H = Integer.valueOf(planets.substring(0, index));
        A = Integer.valueOf(planets.substring(index+1));
        Vector<Integer> humanPlanets = new Vector<Integer>();

        String temp = "1 0";
        for(int i=0;i<H-1;i++){
            temp += " 1 0";
        }

        planets = /*input.nextLine()*/temp;
        System.out.println(planets);
        //planets = input.nextLine();
       int index1 = 0;
       int index2;
       while((index2 = planets.indexOf(' ',index1))!=-1){
           humanPlanets.addElement(Integer.valueOf(planets.substring(index1, index2)));
           index1= index2+1;
       }
       humanPlanets.addElement(Integer.valueOf(planets.substring(index1)));

       Vector<Integer> aliasPlanets = new Vector<Integer>();

       temp = "0 0";
        for(int i=0;i<A-1;i++){
            temp += " 0 0";
        }

       planets = /*input.nextLine()*/temp;
        System.out.println(planets);
       //planets = input.nextLine();
       index1 = 0;
       while((index2 = planets.indexOf(' ',index1))!=-1){
           aliasPlanets.addElement(Integer.valueOf(planets.substring(index1, index2)));
           index1= index2+1;
       }
       aliasPlanets.addElement(Integer.valueOf(planets.substring(index1)));

       int[][] distance = new int[H][A];
       for(int i=0;i<H;i++){

           temp = Integer.toString(random.nextInt(100)+1);
        for(int b=0;b<A-1;b++){
            temp += " " + Integer.toString(random.nextInt(100)+1);
        }

           planets = /*input.nextLine()*//*Integer.toString(random.nextInt(100)+1) + " " + Integer.toString(random.nextInt(100)+1) + " " + Integer.toString(random.nextInt(100)+1)*/temp;

           //planets = input.nextLine();
           index1 = 0;
           int j =0;
           while((index2 = planets.indexOf(' ',index1))!=-1){
               distance[i][j] = Integer.valueOf(planets.substring(index1, index2));
               index1= index2+1;
               j++;
           }
           distance[i][j] = Integer.valueOf(planets.substring(index1));

       }
        if(H>=A){

       int[][] minimumYearsToDefeat = new int[H][A];
       for(int i=0;i<H;i++){
           for(int j=0;j<A;j++){
               double x,y,z;
               y = aliasPlanets.elementAt(j*2) + humanPlanets.elementAt(i*2+1)*distance[i][j]-humanPlanets.elementAt(i*2);
               z = humanPlanets.elementAt(i*2+1) - aliasPlanets.elementAt(j*2+1);
               if(z==0){
                   if(y<=0)
                       x = distance[i][j];
                   else
                       x = Integer.MAX_VALUE;
               }
               else{
               x = y/z;
               }
               if(x==0){
                   x=1;
               }
               else if(x<0){
                   x= Integer.MAX_VALUE;
               }
               minimumYearsToDefeat[i][j] = (int)Math.ceil(x);
           }
       }

       for(int i=0;i<H;i++){
           for(int j=0;j<A;j++)
               System.out.print(minimumYearsToDefeat[i][j]+" ");
           System.out.println();
       }

       int[] mins = new int[A];//meqdar dehi ba big integer
       for(int i=0;i<A;i++){
           mins[i] = Integer.MAX_VALUE;
       }
       int[] minsWith = new int[A];//meqdar dehi ba -1
       for(int i=0;i<A;i++){
           minsWith[i] = -1;
       }

       for(int i=0;i<A;i++){
           for(int j=0;j<H;j++){
               if(minimumYearsToDefeat[j][i]<mins[i]){
                   mins[i] = minimumYearsToDefeat[j][i];
                   minsWith[i] = j;
               }
           }


           for(int p=0;p<A;p++){
               if(minsWith[i]==minsWith[p]&&p!=i&&mins[i]!=Integer.MAX_VALUE){
                   correctingConflict(minimumYearsToDefeat, mins, minsWith, i, p, new Vector<Integer>());
               }
           }


       }
       int result = 0;
       for(int i=0;i<A;i++){
           if (mins[i]>result)
               result = mins[i];

       }
       if(result==Integer.MAX_VALUE){
           System.out.println("IMPOSSIBLE");
       }
       else{
           System.out.println(result);
       }
    }
        else{
            System.out.println("IMPOSSIBLE");
        }
    }
}

public static void correctingConflict(int[][] W, int[] mins, int[] minsWith, int i, int p, Vector<Integer> vector){
    /*for(int v=0;v<A;v++){
    System.out.print(minsWith[v]+" ");
    }
    System.out.println();
    for(int v=0;v<vector.size();v++){
    System.out.print(vector.elementAt(v)+" ");
    }
    System.out.println();*/
    int nextMin1 = Integer.MAX_VALUE;
    int nextMin2 = Integer.MAX_VALUE;
    int nextMinWith1 = minsWith[i];
    int nextMinWith2 = minsWith[p];

    for(int q=0;q<H;q++){
        if(W[q][i]<nextMin1 && W[q][i]>=mins[i] && q!=minsWith[i] && !vector.contains(q)){
            nextMin1 = W[q][i];
            nextMinWith1 = q;
        }

    }
    for(int q=0;q<H;q++){
        if(W[q][p]<nextMin2 && W[q][p]>=mins[p] && q!=minsWith[p] && !vector.contains(q)){
            nextMin2 = W[q][p];
            nextMinWith2 = q;
        }
    }

    if(nextMin1<=nextMin2){
        if (nextMin1==mins[i]) {
            vector.addElement(minsWith[p]);
        } else {
            vector.removeAllElements();
        }
        mins[i] = nextMin1;
        minsWith[i] = nextMinWith1;

        //conflict checking
        for(int s=0;s<A;s++){
               if(minsWith[i]==minsWith[s]&&s!=i&&mins[i]!=Integer.MAX_VALUE){
                   correctingConflict(W, mins, minsWith, i, s, vector);
                   return;
               }
        }

    }
    else if(nextMin2<nextMin1){
        if (nextMin2==mins[p]) {
            vector.removeAllElements();
            vector.addElement(minsWith[p]);
        } else {
            vector.removeAllElements();
        }
        mins[p] = nextMin2;
        minsWith[p] = nextMinWith2;

        //conflict checking
        for(int s=0;s<A;s++){
               if(minsWith[p]==minsWith[s]&&s!=p&&mins[p]!=Integer.MAX_VALUE){
                   correctingConflict(W, mins, minsWith, p, s, vector);
                   return;
               }
        }
    }




}

}

+4  A: 

Well, you haven't shown how you're executing your program or what your program does. I would guess it could be the exit code of the process, although usually if there aren't any errors, the exit code is 0.

If you want a better answer, please provide more information in the question.

Jon Skeet
I`m executing it in NetBeans. the program is a simple program that implements an algorithm for an acm problem, it has inputs and outputs, also it has a recursive function. what I forgot to say is yes when it normally ends the code is 0, but when i manually with the stop button force it to stop, I see an output like this: Java Result: 2147483647BUILD SUCCESSFUL (total time: 8 minutes 49 seconds)What i`m thinking about is if it is any relation between this number and the memory?
Kamran
2^31 == 2147483648. I'm going to guess a -1 has been mangled.
sarnold
ahh! so the result would be either 0 or -1? :-?
Kamran
@sarnold: Well spotted. I'm still not sure what's giving it a non-0 exit code though. @Kamran: Are you sure this isn't actually just what your program is printing?
Jon Skeet
Yes Jon, text`s color is red! meaning its not user-printed!
Kamran
A: 

Let the Java code end explicitly with an System.exit(0)

Thorbjørn Ravn Andersen
actually it is not a problem for me how to exit I`m just wondering what does the number means
Kamran
Thorbjørn's answer is suggesting that without an explicit exit value, perhaps this number is being used in its place. It's definitely worth trying: it is fast and easy to either discover this is it, or rule it out.
sarnold
+2  A: 

You assign Integer.MAX_VALUE in five different places in your code; one of the locations is elements in the mins array.

This code near the end of your main() method can print out values from the mins array:

   int result = 0;
   for(int i=0;i<A;i++){
       if (mins[i]>result)
           result = mins[i];

   }
   if(result==Integer.MAX_VALUE){
       System.out.println("IMPOSSIBLE");
   }
   else{
       System.out.println(result);
   }
}
    else{
        System.out.println("IMPOSSIBLE");
   }

Here's a small test program to find out what Integer.MAX_VALUE actually is:

$ cat test.java ; javac test.java ; java sars
class sars {
    public static void main(String args[]) {
            System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE);
    }
}
Integer.MAX_VALUE: 2147483647

I was wrong in my earlier comment about a -1 leaking through somewhere -- you've embedded the funny value five times into your code. :) I don't know which one is leaking out, or if your algorithm is actually working exactly as it should. Good luck hunting that down :) but at least with your full code posted, someone may help you find what we're both missing.

sarnold
I`m sure the program is not explicitly printing the MAX_VALUE, because it is sure that whatever code is printing it its not mine! its color is red meaning its not user-coded!
Kamran
Can you use some other integer as your sentinel value? Use 16000 instead of `Integer.MAX_VALUE`, and see if your red number is still 2147483647 or if it is 16000 instead?
sarnold
yes! i tried and the number is still 2147483647. another thing to mention is when I wanted to try what you said I did starting and stopping the program several times to get the output! I dont know when it wants to show up?!
Kamran
+1  A: 

If you have a default NetBeans project, and you're launching it from NetBeans, then it's being launched by Ant, using the generated build script that's inside your project directory. To narrow down the problem, try launching the program yourself from the command line:

java acm.mamoth
Mike Baranczak
hm. I Think you are right, I`m going to read Ant`s documentation to get my answer!
Kamran