hi everyone i have a program about the smallest and largest values that user input and the program decide which is the largest which is the smallest ,so i solved it and it worked fine but it's giving me the 2 values are the same the largest same as the smallest i would like just some help or hints comment
thanx in advance

import java.util.Scanner;

public class apples
{

public static void main(String[] args)
{
Scanner input = new Scanner( System.in);

System.out.print ("please enter your first value");
int val = input.nextInt();
int smallest = val; 
int highest = val;
 
for (int count = 0; count < val; count++){
System.out.print ("enter the next value");
 val = input.nextInt();
if (val > highest) highest = val;
System.out.printf ("the highest value is %d",val);
if (val < smallest) smallest = val;
System.out.printf("the lowest value is %d", val);
}

}}

Recommended Answers

All 8 Replies

1)You keep changing the value of "val" variable which is set to the number of loop you want to go through. As a result, your loop is likely to be be unstable. You need to create another variable and use it inside the loop. Don't be too thrifty to create variables.

2)When you initial a value of smallest & highest, you need to initial it to the maximum and minimum value of its type. In this case you use integer, so the values could be...

int smallest = Integer.MAX_VALUE;
int highest = Integer.MIN_VALUE;

If you are going to ask why you need to initial the values that way, you should think about how you compare the value. If you want to find the highest, your first value stored should be the minimum, so it can accept the first input value to be the highest. If you arbitrary initial its value, you could have easily missed the real highest value from user inputs. The same applies to the smallest value.

1)You keep changing the value of "val" variable which is set to the number of loop you want to go through. As a result, your loop is likely to be be unstable. You need to create another variable and use it inside the loop. Don't be too thrifty to create variables.

2)When you initial a value of smallest & highest, you need to initial it to the maximum and minimum value of its type. In this case you use integer, so the values could be...

int smallest = Integer.MAX_VALUE;
int highest = Integer.MIN_VALUE;

If you are going to ask why you need to initial the values that way, you should think about how you compare the value. If you want to find the highest, your first value stored should be the minimum, so it can accept the first input value to be the highest. If you arbitrary initial its value, you could have easily missed the real highest value from user inputs. The same applies to the smallest value.

yes i tried it but it gave me this output:s

please enter your first value3
enter the next value2
the highest value is 2147483647the lowest value is -2147483648enter the next value

another question why you put Integer.max_value and same to the min_value ?

Uhm ...

Arrays.sort(arrayVar)

and take the first and last?

Edit: Nevermind, that's being a bit facetious. ;-)

Why don't you try posting your newest code, as well as a cut-n-paste copy of your attempt at running it?

another question why you put Integer.max_value and same to the min_value ?

You need to go back and look at my post again... I assigned "maximum" to your "smallest" and "minimum" to your "highest." I also tried to explain why I did that... But the result shows that you are doing the opposite.

You need to go back and look at my post again... I assigned "maximum" to your "smallest" and "minimum" to your "highest." I also tried to explain why I did that... But the result shows that you are doing the opposite.

oppppppppppps yes i didn't read it well thanx anyway i will try it now and check i tried it with the code you gave it to me it's still printing me aloot of numbers not the numbers i input it here is the program

import java.util.Scanner;

public class apples
{

public static void main(String[] args)
{
Scanner input = new Scanner( System.in);

System.out.print ("please enter your first value");
int val = input.nextInt();
int smallest = Integer.MAX_VALUE;
int highest = Integer.MIN_VALUE;
for (int count = 0; count < val; count++){
System.out.print ("enter the next value");
 val = input.nextInt();
if (val > highest) highest = Integer.MIN_VALUE;
System.out.printf ("the highest value is %d",smallest);
if (val < smallest) smallest = Integer.MAX_VALUE  ;
System.out.printf("the lowest value is %d",highest);
}

}}

Uhm ...

Arrays.sort(arrayVar)

and take the first and last?

Edit: Nevermind, that's being a bit facetious. ;-)

Why don't you try posting your newest code, as well as a cut-n-paste copy of your attempt at running it?

ok this is my newest code

import java.util.Scanner;

public class apples
{

public static void main(String[] args)
{
Scanner input = new Scanner( System.in);

System.out.print ("please enter your first value");
int val = input.nextInt();
int smallest = Integer.MAX_VALUE;
int highest = Integer.MIN_VALUE;
for (int count = 0; count < val; count++){
System.out.print ("enter the next value");
 val = input.nextInt();
if (val > highest) highest = Integer.MIN_VALUE;
System.out.printf ("the highest value is %d",smallest);
if (val < smallest) smallest = Integer.MAX_VALUE  ;
System.out.printf("the lowest value is %d",highest);
}

}}

AND THIS IS THE OUTPUT IT GIVES ME EVERYTIME I TRY TO RUN IT

please enter your first value3
enter the next value7
the highest value is 2147483647the lowest value is -2147483648enter the next value

Because you keep assigning the MIN and MAX value again and again after you compare... You need to assign the compared value, not the constant!

// this
if (val > highest) highest = Integer.MIN_VALUE;
// should be
if (val > highest) highest = val;

PS: It is rude to have all capitalized. I volunteer to help you and not ask for anything back. I also EXPLAINED WHAT VALUE TO BE ASSIGNED in my earlier post as well.

Because you keep assigning the MIN and MAX value again and again after you compare... You need to assign the compared value, not the constant!

// this
if (val > highest) highest = Integer.MIN_VALUE;
// should be
if (val > highest) highest = val;

PS: It is rude to have all capitalized. I volunteer to help you and not ask for anything back. I also EXPLAINED WHAT VALUE TO BE ASSIGNED in my earlier post as well.

iam really soo sorry i didn't mean anything i was in a hurry and i didn't check it's capital letter i swear iam sorry again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.