Java Code

I am trying to return the lowest number from a string of 10 numbers that the user inputs. Here is my code. I'm not getting any errors it just doesn't seem to want to move to the last step of printing the lowest number. Here is my code:

    System.out.print("Enter 10 numbers: ");
    double [] num = new double [10];
    int j;
    double finmin = 0;
       
    for(j=0; j < num.length; j++){
       num[j] = input.nextDouble();
       finmin = min(num);
    }
   
   System.out.print("your lowest number is: " + finmin);
    }

    /**
     *
     * @param num
     * @return
     */
    public static double min(double[] num)
   {
        int j;
        double finmin = 0;
        for (j = 0; j < num.length; j++){
            if (num[j] < j)
               finmin = num[j];
            else
               finmin = num[j];
            }
        return (finmin);
lostinspace9Asked:
Who is Participating?
 
Sharon SethConnect With a Mentor Commented:
Try this:
public static double min(double[] num)
   {
        int j;
        double finmin = num[0];
        for (j = 0; j < num.length; j++){
            if (num[j] < finmin)
               finmin = num[j];
           }
        return (finmin); 
 }

Open in new window

and call min() outside the for loop
0
 
plusone3055Commented:
have you tried changing  

double finmin = 0;
to
int finmin = 0;
0
 
lostinspace9Author Commented:
when I do that, it makes my assignment statements incorrect. I need to keep the return type as a double.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mrcoffee365Commented:
There are some weird things.  You calculate the minimum as you read each element in the array?  I'd think you want to call min after you fill the array, not for each element in it.  So take
finmin =
out of the read loop.

When you say it doesn't get to printing the finmin value -- you mean it hangs, right?  The program never ends?

So you have a more serious problem than odd computing.  Do you have a log with an exception in it?  Otherwise, I'd say that it is in an infinite loop somewhere.
0
 
krakatoaConnect With a Mentor Commented:
AFAICS all you need is to change things slightly:

import java.util.*;

class Mmin{ 



public static void main(String[] args){


Scanner input = new Scanner(System.in);

System.out.print("Enter 10 numbers: ");
    double [] num = new double [10];
    int j;
    double finmin = 0;
       
    for(j=0; j < num.length; j++){
       num[j] = input.nextDouble();
       finmin = min(num);
    } 
   
   System.out.print("your lowest number is: " + (int)finmin);
    
}

    
    public static double min(double[] num)
   {
        int j;
        double finmin = 0;
        for (j = 0; j < num.length; j++){
            if (num[j] < j){
               finmin = num[j];
	}
            else{
               finmin = num[j];
	}
            }
        return (finmin);
}
}

Open in new window

0
 
mrcoffee365Commented:
But for the minimum -- you're comparing the input double value to the index into the array.  You don't want to know if the input value is lower than the index, you want to know if it's lower than the lowest value you've gotten from the array so far.

The way that's usually done is to create the lowest found val above the loop, then compare each element to that val.  If the element is lower, set the lowest found val to the element.
0
 
krakatoaCommented:
Or you can miss out your min method entirely :

import java.util.*;

class Mmin{ 



public static void main(String[] args){


Scanner input = new Scanner(System.in);

System.out.print("Enter 10 numbers: ");
    double [] num = new double [10];
    int j;
  
       
    for(j=0; j < num.length; j++){
       num[j] = input.nextDouble();
      
    } 
   Arrays.sort(num);
System.out.println((int)num[0]);
  
    
}

}

Open in new window

0
 
mrcoffee365Commented:
krakatoa:  This is clearly homework.  It would be best not to provide a whole program to people doing homework.
0
 
lostinspace9Author Commented:
krakatoa, I am not looking for someone to write it for me..As you can see, I have written the majority of it myself and am looking for help in correcting what I have already written. Thank you for your input.
0
 
krakatoaCommented:
mrcoffee365 & lostinspace9 :

What I have observed over the years on this site has always been the convention that if an Asker had offered code, whether it contained large or small errors, it was  accepted that this was enough to allow someone else to come along with corrections. That's all I have done here.

If you look at what the OP posted as code, all that was lacking in this case was some bracketing and braces - in no way did the overall flow fall short of the target, except that it wouldn't compile - (which is actually a trivial mistake). If you look at what I posted, it wasn't really much of a correction even,and my second code post is merely a reference to an API function, showing that it's possible to do the same thing with a trivial call to Arrays.sort(). I'd've thought all that was fair game.

Anyway - don't gripe at me - sharonseth posted code too, so why's that so different??
0
 
krakatoaCommented:
Here's another comment for you -

If you can write code that gets as close as your code did to giving you the correct answer, then perhaps you can explain to this audience how it's possible that you can do so without writing it so that it can compile due to incorrect bracketing?

PLEASE don't tell me you need help with your homework based on that!!
0
 
lostinspace9Author Commented:
I can do so because I am human and I am not perfect and I am by no means an expert programmer. I don't need the code...as I have already stated. I need help in figuring out why, as it was stated, I am stuck in a loop. Sometime things simply ARE NOT obvious when you have been looking at them for days. This is why I PAY for help on this site. I DO NOT PAY to have offensive comments thrown my direction. I am NOT a new user to Experts Exchange and I do not appreciate your comments.
Thank you to those who have offered up help. I will be applying the ideas suggested to see if I can get my code running.
0
 
mrcoffee365Commented:
lostinspace9:  I apologize on behalf of the volunteers on this site who try to answer questions.  You asked a question, I think you have some good answers, good luck in your endeavors.  Usually questions don't devolve in this way, so please try another question if you need help again.

krakatoa: seriously?  I think you need to dial it back a bit.  It's never cool to make ad hominem statements.  I've seen you help people, just keep it to that.
0
 
krakatoaCommented:
I need help in figuring out why, as it was stated, I am stuck in a loop.

I don't see how you can be stuck in a loop, when the code you have posted will not compile. How do you get the programme running at all?

That was the point of my posts - I haven't changed the code at all, I've simply put in the correct bracketing. The only thing you didn't seem to want from the first expert's suggestion, was the int / double solution, as you said your aim was to deal with a double. My amended code returns your double, and just casts it to an int. That's about the biggest change my contribution entails.
0
 
Sharon SethCommented:
@lostinspace9 : Just try the code I posted and study the logic there
0
 
lostinspace9Author Commented:
The solution is good. I still had some problems getting my output correct but I got out of my loop, so thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.