I have no hair left :(

 

I need help so bad, i know your not slaves but please help me i have been pulling my hair out over this thing and i know

its something little and i have to be shown it or hinted at least. i have watched youtube tutorials, read my text and still no success

oh but i can make a EA in MQL4 no probs....i feel so embarrased to post this here. Its in java but i know you can help me. I need to loop

the function getPositiveAmount so that when a user enters a number i call that function to check if the number is negative "<0". I am new to programing

import java.util.Scanner;

public class assignmenttwo 
{
        
        public static double getPositiveAmount()   
        {       
                    
            Scanner reader=new Scanner(System.in);
            double input=reader.nextDouble();      
                
            if (input<0)
            {
                System.out.println("Please enter a positive value: ");
            }
            
                      
             return input;  
        }
        

        
        public static void runSimulation()   
        {      
                
                
                
                System.out.println("Welcome to the Bank Interest Simulator");
                System.out.print("Enter initial balance ($): ");
                getPositiveAmount();
                Scanner reader=new Scanner(System.in);
                double initialbal=reader.nextDouble();
                System.out.print("Enter target value ($): ");
                getPositiveAmount();
                double targetbal=reader.nextDouble();
                
                
                
                

                
                   

        }   



        // Do not change   
        public static void main(String args[])   
        {      
                runSimulation();   
        }
}
 

this is what i get

Welcome to the Bank Interest Simulator

Enter initial balance ($): -1 (user enters -1)

Please enter a positive value: -3 (user enters -3)

***

Enter target balance ($):

------------------------------------

where the stars are it should say "Please enter a positive value:" again

This is what i need to know how to do i need the getPositiveAmount function to check the number

again to make sure its not negative infinetly in the user keeps entering negative numbers. Sorry to be a pain in the ass

 

I don't know java . . . but what about . . .

System.out.print("Enter target value ($): ");

while(getPositiveAmount() < 0)
   {
   sleep(200);
   }

double targetbal=reader.nextDouble();
 

i have tried using a while statement instead of the if and i just get the message "Please enter a positive value" in a big list down my page infinetly

 

Your fast RaptorUK ill try

 

Ah you want java code:

Note that in general using a do{}while loop is not a good idea because it is easy to loose track.

voila:

public class assignmenttwo  {

    private static double readDouble(boolean needPostive) {
        double retVal = 0;
        Scanner s = new Scanner(System.in);
        do {
            if (retVal < 0&&needPostive) {
                System.out.println("Please enter a positive value: ");
            }
            retVal = s.nextDouble();
        } while (retVal < 0&&needPostive);
        return (retVal);
    }

    private static void runSimulation() {
        System.out.println("Welcome to the Bank Interest Simulator");
        System.out.print("Enter initial balance ($): ");
        double initialBalance=readDouble(true);
        System.out.print("Enter target value ($): ");
        double targetBalance=readDouble(false);
    }

// Do not change   
    public static void main(String args[]) {
        runSimulation();
    }
}
 

Additionally you should give your bets to understand why the above solution work. Using if's and while's is the most basic stuff in every classical programming language.


add: readDouble funtion with a while loop:

    private static double readDouble2(boolean needPositive){
        double retVal=0;
        while(retVal<=0){
            retVal=new Scanner(System.in).nextDouble();
            if(needPositive){
                if(retVal<0)System.out.println("Please enter a positive value: ");
            }else{
                return retVal;
            }
        }
        return retVal;
    }
 

no luck, this is for a uni assignment so annoying this is the 3rd night i have spent on this little problem about 15hrs lol

 

Thanks for the help zzuegg but i amlearning the very basic's of java lol, i can make out what you have written above but i dont think my teacher will believe its my work if submit anything like it...

double initialBalance=readDouble(true); does this line take a value from the function readDouble and assign it to "initialbalance" if "needpositive"=true

also do you need to use boolean true/false always when dealing with while statements. only answer if you want to i dont want to be a hassel to anyone

 

Well, i guess you cannot make the program more simple if you want to reach the goal.

double initialBalance=readDouble(true); // assigns the value returned by the function readDouble to the variable initialBalance

Well, i could answer you in detail what every line does, but thats the problem, you HAVE TO understand that yourself, if you don't you will probably fail in the next assignment.

Btw, the above code can be nearly read as plain english. Leave out the declarations and function declarations and you have a nearly english text in front of you.

reading the tutorials is always a good idea, and when you progress, get used to javadoc. It will save you most of the times hours of research.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

At last, get a good IDE for developing, the 2 most used for java development are eclipse and netbeans. They also will save you hours of development time. (As more complex things you do, as more time they save). Tho in the university we used eclipse i prefer netbeans because of the better plugin's available.

Instead of trying every possible combination of code, it is better to read tutorials until you understand what you need.

 

thank you for your help i managed to get most of it done, the other 85% in about 2 hours, I really like MQL4 as a language it is ... easy on the eyes. Heres what i have so far, im sure i can get the whole "checking for a negative user input" section eventually. I will have to use a while statement definetly. I used your way of calling the getPositiveAmount() function its commented out atm is it correct. Cant wait to finish this. I will post once all working to show how helpful the people are here. thanks

import java.util.Scanner;

public class freshcopy
{

public static double getPositiveAmount()
   {
      double input;
      Scanner reader=new Scanner(System.in);
      input=reader.nextDouble();

      while (input<0)                   // this part is killing me
      {
          System.out.println("please enter a positive value: ");
      }
      
    return input;       
   }
     
          
      
                  
                  
public static void runSimulation()
      {
          double initialbalance;
          double targetbalance;
          double interestrate;
          double newbalance;
          Scanner reader=new Scanner(System.in);
      System.out.println("Welcome to the Bank Interest Simulator");
      System.out.print("Enter initial balance ($): ");
      //initialbalance=getPositiveAmount();
      initialbalance=reader.nextDouble();
      System.out.print("Enter target balance ($): ");
      //targetbalance=getPositiveAmount();
      targetbalance=reader.nextDouble();
      System.out.print("Enter interest rate (%): ");
      //interestrate=getPositiveAmount();
      interestrate=reader.nextDouble();
      
      
          
          
          
          int year=0;
          while (initialbalance<=targetbalance)
          {
                  
                  initialbalance=(initialbalance*(interestrate/100))+initialbalance;
                  System.out.print("Balance at end of year " );
                  year++;
                  System.out.print(year);
                  System.out.print(": ");
                  System.out.println(initialbalance);
          }
          
          
       System.out.print("Number of years to reach (or exceed) target ");
       System.out.print(year);
      
      
      
      
      
      
      
   }



   // Do not change
   public static void main(String args[])
   {
      runSimulation();
   }
}