# min and max value of int

• October 21st, 2013, 10:31 AM
Oldemor
min and max value of int
Hey, im trying to make a program where you can type in alot of values, and then it should return the sum of them, how many values ive typed in and the highest/lowest value. My code currently looks like this
Code :

```import java.util.Scanner;     public class OppgaveC {   static double maksTemp = Integer.MAX_VALUE; static double minTemp = Integer.MIN_VALUE; static double sum = 0; static int antallTemp = 0;   public static void main(String[] args) { temperaturer(args); }     public static void temperaturer(String[] args) {     System.out.println("Skriv temperaturer");   Scanner in = new Scanner(System.in);   System.out.println(fåSvar(in));   System.out.println(maksTemp);   }   public static double fåSvar(Scanner in){   boolean kjør = true;   while (kjør == true) {   double fåTemperaturer = in.nextDouble();   if (fåTemperaturer == -100) kjør = false; else { if (fåTemperaturer > maksTemp) maksTemp = fåTemperaturer; if (fåTemperaturer <= minTemp) minTemp = fåTemperaturer;   sum+= fåTemperaturer; antallTemp++; } } return sum;       } }```

Problems im having, it wont return the correct min or max value, and im yet to figure out how to make it return all the different values at the same time :) Any thoughts?
• October 21st, 2013, 10:35 AM
PhHein
Re: min and max value of int
Look at the initial values of maksTemp and minTemp and how you compare them.
• October 21st, 2013, 10:43 AM
Oldemor
Re: min and max value of int
Wow, it worked by just changing their value to 0 instead of the integer.max_value and min_value.. Thank you (~~)(~~)
• October 21st, 2013, 10:44 AM
PhHein
Re: min and max value of int
Now try by entering only negative tempreatures. Hint: it won't work.
• October 21st, 2013, 10:47 AM
Oldemor
Re: min and max value of int
:eek: You're right, wanna help me on this one?
• October 21st, 2013, 10:50 AM
PhHein
Re: min and max value of int
Just swap your initial values. Your maxTemp should be greater than Integer.MIN, no?
• October 21st, 2013, 10:55 AM
Oldemor
Re: min and max value of int
It seems to be working like it should, without doing the modifications youre suggesting.. hmm.

--- Update ---

edit: reason i said it wasnt working in the start was because i had some random errors somehow, but now the same code is working like it should
• October 21st, 2013, 03:59 PM
Oldemor
Re: min and max value of int
My code now looks like this:

Code :

```import java.util.Scanner;     public class OppgaveC {   static double maksTemp = 0; static double minTemp = 0; static double sum = 0; static int antallTemp = 0; static boolean kjør = true;   public static void main(String[] args) { temperaturer(args); }     public static void temperaturer(String[] args) {     System.out.println("Write temperatures, write -100 to finish");   Scanner in = new Scanner(System.in);   System.out.println(fåSvar(in));   System.out.println("Highest temperature: " + maksTemp); System.out.println("Lowest temperature: " + minTemp); System.out.println("Avg temp: " + (sum/antallTemp)); System.out.println("Number of registered temperatures: " + antallTemp);   }   public static double fåSvar(Scanner in){   while (kjør == true) {   double fåTemperaturer = in.nextDouble();   if (fåTemperaturer == -100) kjør = false; else { if (fåTemperaturer > maksTemp) maksTemp = fåTemperaturer; if (fåTemperaturer <= minTemp) minTemp = fåTemperaturer;   sum+= fåTemperaturer; antallTemp++; } } return 0;       } }```

Is there any other way to solve it? Cus atm i really dont want it to return 0, but it has to return something in order to work. I HAVE to have a Scanner as a parameter. Any thoughts?

Is there a better and more clean way to do this? Cus atm i feel like its a bit of a mess.
• October 22nd, 2013, 01:15 AM
Junky
Re: min and max value of int
What is the smallest value an int can have? Will all values entered be greater than that value?
• October 22nd, 2013, 02:18 AM
Oldemor
Re: min and max value of int
2147483647! But ive fixed that, now i need to fix or remake the program so it do exactly the same without returning a 0 or a random value that should be shown with a println in the main
• October 22nd, 2013, 04:07 AM
Junky
Re: min and max value of int
If you don't want it to return 0 then why do you have that statement there? What should it return instead? I have no idea. What is the method supposed to do? Looking at your code, no matter what happens the method will ALWAYS return 0. It seems an odd behavior to me.
• October 22nd, 2013, 05:14 AM
Oldemor
Re: min and max value of int
Yeah, im sorry, im having a hard time saying what i really mean, when my native language isnt english :) I got a little guidance from my mentor, and i had just missunderstood the task i'd gotten. This is how its supposed to look:

Code :

```import java.util.Scanner;   //oppgave C   public class OppgaveF {     public static void main(String[] args) {   Scanner input = new Scanner(System.in); temperaturer(input); }   private static void temperaturer(Scanner input) { // TODO Auto-generated method stub double minTemp = 0; double maxTemp = 0; double temp; double sumTemp = 0; int tempCount = 0;   do {   System.out.println("-> " ); temp = input.nextDouble();   if (tempCount == 0) { maxTemp = temp; minTemp = temp; }   if (temp < minTemp && temp != -100) {   minTemp = temp; }   if (temp > maxTemp) { maxTemp = temp; }   if (temp != -100) { tempCount++; sumTemp += temp; } }   while (temp != -100);   System.out.println(); System.out.println(); System.out.println(); System.out.println();   input.close();   }     }```