1 Reply Latest reply: Nov 19, 2012 12:59 AM by sabre150 RSS

    infinite loop ;(

    974869
      I've got an infinite loop somewhere, & I'm not quite sure where.
      Also when entering in a string for the name of the company, I can't put a space in my string name. I never really thought about that until now.
      Here's the code:
      import java.util.Scanner;
      public class Stock {
          
          private String symbol;
          private String name;
          private double previousClosingPrice;
          private double currentPrice;
          
          
              
          //No-Arg constructor prompts the user to enter all 4 data members
          //Make sure that current and previous price are greater than 0
          public Stock(){
              Scanner input = new Scanner(System.in);
              System.out.println("Please enter symbol: ");
              symbol = input.next();
              System.out.println("Please enter name: ");
              name = input.next();
              System.out.println("Please enter previous closing price greater than 0: ");
              previousClosingPrice = input.nextDouble();
              if(previousClosingPrice < 0){
                 System.out.println("Please enter previous closing price greater than 0: ");
                 previousClosingPrice = input.nextDouble();
              }
              System.out.println("Please enter the current price: ");
              currentPrice = input.nextDouble();
              if(currentPrice < 0){
                 System.out.println("Please enter a current price greater than 0: ");
                 currentPrice = input.nextDouble();
              }
          }
      Here is my main:
      public class Lab8Client{
          public static void main(String[] args){
              /*         
              Make sure you have completed the Stock class definition before
              completing the following items
              */
              
              /* Step 1:
               * Create two Stock objects. For the first, use the no-arg constructor
               * and use the data for oracle corp shown in the assignment. For the 
               * second, use the constructor with arguments providing a stock symbol 
               * and name of your choice. Set the two price values for the second 
               * stock using the set methods. Display the stock info (using toString)
               * and the percent change for both stocks, (formatting the percent).
               */
              String Goog;
              String Google;          
              Stock firstStock = new Stock();
              Stock secondStock = new Stock("Goog", "Google");
              secondStock.setPreviousClosingPrice(20000.00);
              secondStock.setCurrentPrice(40000.00);
              System.out.println("First Stock info " + firstStock.toString());
              System.out.println("Second Stock info " + secondStock.toString());
              System.out.println("First Stock percent change:" + firstStock.getChangePercent() 
                      + " \nSecond Stock precent change:" + secondStock.getChangePercent());  
                     
              /*
               * Step 2: Test the equals method. Show two tests, one where the stocks
               * are equal, one where they are not.
               */
              if(firstStock.equals(secondStock)){
                  System.out.println("Stocks percent change is equal.");
              }
              else {
                  System.out.println("Stocks percent change isn't equal.");
              }
              
              /*
               * Step 3: Declare an array of 5 Stock objects. Initialize the array 
               * elements with Stock objects, allowing the user to enter the data
               * for each object. Output all Stocks in the array using the toString
               * method.
               */
              Stock[] stockArray = new Stock[5];
              for(int i = 0; i < stockArray.length; i++){
                  stockArray[i] = new Stock();
              }       
              /*
               * Step 4: Write a printStocks method that accepts the stock array and 
               * prints all of the Stocks using the to String method. Call your method
               * here. You can remove the display of the stocks from Step 3.
               */
              printStocks(stockArray);
              printLowAndHighPercentChange(stockArray);
          }   
          public static void printStocks(Stock[] stockArray){
              for(int i = 0; i < stockArray.length; i++){
                  stockArray[i] = new Stock();
              }
              System.out.println("Third Stock info " + stockArray[0].toString());
              System.out.println("Fourth Stock info " + stockArray[1].toString());
              System.out.println("Fifth Stock info " + stockArray[2].toString());
              System.out.println("Sixth Stock info " + stockArray[3].toString());
              System.out.println("Seventh Stock info " + stockArray[4].toString());
                
          }
              
              /* Step 5: Find and report on which stock had the lowest percentage 
               * change, and which stock had the highest percentage change.
              */   
          public static void printLowAndHighPercentChange(Stock[] stockArray){
              double min;
              double max;
              for(int i = 0; i < stockArray.length; i++){
                  stockArray[i] = new Stock();
                  min = stockArray.getChangePercent();
      if(stockArray[i].getChangePercent() < min){
      min = stockArray[i].getChangePercent();
      }
      System.out.println("Stock " + stockArray[i] + "had the lowest percent change at: " + min);
      }
      for(int i = 0; i < stockArray.length; i++){
      stockArray[i] = new Stock();
      max = stockArray[i].getChangePercent();
      if(stockArray[i].getChangePercent() > max){
      max = stockArray[i].getChangePercent();
      }
      System.out.println("Stock " + stockArray[i] + "had the highest percent change at: " + max);
      }
      }
      }



      Thanks!!

      Edited by: sabre150 on Nov 19, 2012 6:54 AM

      Moderator action : added [code ] tags to format the code and make it readable. Please do this for yourself in the future.