?
Solved

Java Code

Posted on 2013-11-01
16
Medium Priority
?
295 Views
Last Modified: 2013-11-06
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);
0
Comment
Question by:lostinspace9
  • 5
  • 4
  • 4
  • +2
16 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 39617403
have you tried changing  

double finmin = 0;
to
int finmin = 0;
0
 

Author Comment

by:lostinspace9
ID: 39617418
when I do that, it makes my assignment statements incorrect. I need to keep the return type as a double.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 39617736
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Assisted Solution

by:krakatoa
krakatoa earned 600 total points
ID: 39617757
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
 
LVL 12

Accepted Solution

by:
Sharon Seth earned 1400 total points
ID: 39617760
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 39617761
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39617788
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 39617960
krakatoa:  This is clearly homework.  It would be best not to provide a whole program to people doing homework.
0
 

Author Comment

by:lostinspace9
ID: 39618155
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39618179
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39618186
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
 

Author Comment

by:lostinspace9
ID: 39618244
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 39618253
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39618604
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
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 39619754
@lostinspace9 : Just try the code I posted and study the logic there
0
 

Author Closing Comment

by:lostinspace9
ID: 39628308
The solution is good. I still had some problems getting my output correct but I got out of my loop, so thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question