Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Code

Posted on 2013-11-01
16
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 16

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 16

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 16

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 16

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 16

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

610 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