Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: min and max value of int

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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
    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?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: min and max value of int

    Look at the initial values of maksTemp and minTemp and how you compare them.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: min and max value of int

    Now try by entering only negative tempreatures. Hint: it won't work.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: min and max value of int

    You're right, wanna help me on this one?

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: min and max value of int

    Just swap your initial values. Your maxTemp should be greater than Integer.MIN, no?

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: min and max value of int

    My code now looks like this:

    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.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

  10. #10
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  12. #12
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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:

    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();
     
    	}
     
     
    }

    Thanks for answering my thread anyway, really appreciated

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: min and max value of int

    It's better to start the minTemp initialized at a LARGE value, like the MAX_VALUE of int. That way, the first temperature found less than (smaller than) minTemp will become the new minTemp. From there, any temp found smaller than minTemp will become the new minTemp. A general rule: Initialize variables to capture min values very large, initialize variables to capture max values very small.

    Since you've initialized minTemp to 0, if you feed the program only positive temps, the minTemp will remain zero.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    Oldemor (October 22nd, 2013)

  15. #14
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: min and max value of int

    Ill try it when i come home, thanks for the advice

Similar Threads

  1. Min/Max of fields
    By jbraque in forum Object Oriented Programming
    Replies: 1
    Last Post: May 27th, 2013, 03:58 PM
  2. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 08:50 PM
  3. Need help with C++ min and max heaps.
    By javapenguin in forum Other Programming Languages
    Replies: 6
    Last Post: October 19th, 2011, 11:08 AM
  4. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  5. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM