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 4 of 4

Thread: Finding the lowest integer using sentinellvalue. :( I am stuck!

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Finding the lowest integer using sentinellvalue. :( I am stuck!

    Hello All,

    I am trying to find the lowest integer. I want the user to enter a set of integers and then have the user enter a value to exit the program and have the lowest integer display.

    So far, I have the user enter 4 integers and for the fifth integer, the user enters -1. The lowest integer displays.

    But what if the user wants to enter 3 integers and have the lowest display?

    I don't know what to do in order to make this program work I have sat here at my PC for hours trying to plug in IF statements but nothing works.. ugh!

    Here is my code so far:




    import java.util.Scanner;
    public class Lowjb
    {
    	public static void main(String [] args)
    	{
    		double num1;
    		double num2;
    		double num3;
    		double num4;
    		double num5= 0;
    		double numberCounter;
     
    		numberCounter = 0;
     
    		Scanner input = new Scanner(System.in);
     
    		while(num5!=-1)
    		{
    		System.out.println("Please enter your first number or enter -1 to exit: " );
    		num1 = input.nextDouble();
     
    		System.out.println("Please enter your second number or enter -1 to exit: " );
    		num2 = input.nextDouble();
     
    		System.out.println("Please enter your third number or enter -1 to exit: " );
    		num3 = input.nextDouble();
     
    		System.out.println("Please enter the fourth number or enter -1 to exit: " );
    		num4 = input.nextDouble();
     
    		System.out.println("Please enter the fifth number or enter -1 to exit: " );
    		num5 = input.nextDouble();
     
     
    		double result = minimum(num1, num2, num3, num4);
    		System.out.println("The minimum value of the group of numbers is: " + result);
     
    		numberCounter = numberCounter+1;
    		}
    	}
     
    	public static double minimum(double a, double b, double c, double d)
    	{
    		double minimumValue = a;
     
    		if(b < a)
    			minimumValue = b;
     
    		if(c < a)
    			minimumValue = c;
     
    		if(d < a)
    			minimumValue = d;
     
    		return minimumValue;
     
    	}
    }
    Last edited by Norm; March 7th, 2013 at 07:12 AM. Reason: zero change to letter O


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Finding the lowest integer using sentinellvalue. :( I am stuck!

    You should use dynamic structures in this case:

    public static void main(String[] args) {
    		ArrayList<Integer> inputVals = new ArrayList<>();
    		Integer temp = 0;
    		Scanner scanner = new Scanner(System.in);
    		do {
    			temp = scanner.nextInt();
    			if (temp != -1)
    				inputVals.add(temp);
    		} while (temp != -1);
    		Collections.sort(inputVals);
    		if (inputVals.size() > 0)
    			System.out.println(inputVals.get(0));
     
    	}

    Look at above. I use an arraylist. I dont know if you are familiar with it but think of an arraylist similar as an array with a type it should store(better explenation here). Then you can add as many values (of the type) as you want. So you can store three, four five and so on inputs. Integer now has the advantage that it implements the comparable-interface. The static method sort of the class-library Collections now sorts the arraylist and you only must print out the (lowest) value at position one.

    Dont know how helpful the post is. If you have questions, post it. But please try to read the three links above first.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Finding the lowest integer using sentinellvalue. :( I am stuck!

    Quote Originally Posted by StrugglerWithJava View Post
    ... I want the user to enter a set of integers and then have the user enter a value to exit the program and have the lowest integer display....
    I don't see anything in the problem statement that says you have to save all of the values.

    So...

    How about just looking at each entry as it comes in? If it's -1, then quit. If it is not -1, see whether it is smaller than the previous smallest entry.

    Maybe something like:
    Declare and initialize the Scanner object
     
    Declare int variables num and smallest.
     
    Prompt the user to enter some integers.  Tell
    the user that input will be terminated when
    -1 is entered.
     
    Set num equal to the next int obtained from the Scanner object.
    Set smallest equal to num.
     
    Execute the following loop as long as num is not equal to -1:
    BEGIN LOOP
        IF num is less than smallest
        THEN
            Set smallest equal to num.
        END IF
        Prompt the user for the next entry.
        Set num equal to the next int obtained from the Scanner object.
    END LOOP
     
    Report results to the user.


    A couple of runs could look something like:

    Enter some integers.  To terminate input, enter -1
     
    Enter an integer : 1
    Enter another integer (Enter -1 to terminate): 3000
    Enter another integer (Enter -1 to terminate): -1000000
    Enter another integer (Enter -1 to terminate): 55
    Enter another integer (Enter -1 to terminate): -1
     
    The smallest integer that you entered (before the -1) was -1000000


    And

    Enter some integers.  To terminate input, enter -1
     
    Enter an integer : -1
     
    The only entry you made was -1



    Cheers!

    Z

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Finding the lowest integer using sentinellvalue. :( I am stuck!

    Of course!!! You are right. There I was a little bit blockheaded

Similar Threads

  1. Finding the lowest number in an array.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 01:49 AM
  2. Finding a Smallest Integer
    By antnas in forum Collections and Generics
    Replies: 5
    Last Post: November 1st, 2012, 01:15 AM
  3. Get the lowest value from Object[][]
    By Tyluur in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 19th, 2012, 10:55 AM
  4. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM
  5. Finding the range of 2 integer inputs
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2012, 02:54 PM