Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Need Help looping through an Array

    I need help looping through an array. I have tried many different ways to do the array and it doesnt seem to show any information this is my code: Highlighted the red area at which I need help! All feedback is greatly appreciated.


    Java Code:
    
    
    
    
    import java.util.Scanner;
    
    
    
    public class Sexton_IT251_P3IP {
        
        
        public static void main(String[] args) {
            
      
            Accounts Accounts1 =  new Paper();
    
            Services Services1 = new Services();
    
            Paper Paper1 = new Paper();
    
            Supplies Supplies1 = new Supplies();     
            
           
            Scanner input = new Scanner(System.in);
            
    
            System.out.println("Account Application: ");    
    
            System.out.print("Please enter the account ID: ");
            Accounts1.setAccountId(input.nextInt());
            
            System.out.print("Please enter the product ID: ");
            Accounts1.setProductId(input.nextInt());
           
            System.out.println("\n\nPaper Products: ");
    
            System.out.print("Please enter the amount of pounds sold: ");
            Paper1.setPoundsSold(input.nextDouble());
            
    
            System.out.print("Please enter the price per a pound: ");
            Paper1.setPricePerPound(input.nextDouble());
            
    
            System.out.println("\n\nService Products: ");
    
            System.out.print("Please enter the number of hours: ");
            Services1.setNumberHours(input.nextInt());
            
    
            System.out.print("Please enter the rate per hour: ");
            Services1.setRatePerHour(input.nextDouble());
            
    
            System.out.println("\n\nSupply Products: ");
    
            System.out.print("Please enter the sales amount of office supplies sold: ");
            Supplies1.setOfficeSupplies(input.nextDouble());
            
    
            System.out.print("Please enter the sales amount of apparel sold: ");
            Supplies1.setApparel(input.nextDouble());
            
    
            System.out.print("Please enter the sales amount of books sold: ");     
            Supplies1.setBook(input.nextDouble());
    
    
              
            Accounts[] array = new Accounts [3];
            
            array[0] = new Paper();
            array[1] = new Services();
            array[2] = new Supplies();
            
            for(Accounts x: array){
                
                array[x].computeSales();
                
            }
            
            
            Accounts[] array1 = new Accounts[3];
            
            array1[0] = new Paper();
            array1[1] = new Services();
            array1[2] = new Supplies();
            
            for (Accounts x: array){
                
                array1[x].toString();
                
            }
            
        }//End method
        
    }//End class
    
    
    
    
    
    
    public class Services extends Accounts{
        
    
        private int numberHours;
    
        private double ratePerHour;
       
    
        public int getNumberHours(){
            
    
            return numberHours;
       }
        
    
        public void setNumberHours(int numberHours){
            
    
            this.numberHours = numberHours;
        }
        
    
        public double getRatePerhour(){
            
    
            return ratePerHour;
        }
        
    
        public void setRatePerHour(double ratePerHour){
            
    
            this.ratePerHour = ratePerHour;
        }
    
        @Override
    
        public String toString(){
            
    
            return String.format("Service Sales: \n" + "Number of service hours: " + 
                    numberHours + "\nService rate per hour:  " + ratePerHour);
            
        }
        
        
        @Override
        public void computeSales(){
            
     
            double calculation = this.numberHours * this.ratePerHour;
            
    
            System.out.println("The current sales amount for services provided: " +
            calculation);       
        }
            
    }
    
    
    
    
    
    
    
    
    //Create public class Accounts
    public abstract class Accounts {
      
      //Initialize private int variable accountId
      public int accountId;
      //Initialize private int variable productId
      public int productId;
      
       
       //Create public method getAccountId that returns an int value
       public int getAccountId(){
           
           //Return accountId
           return accountId;
       }//End method
       
       //Create public method setAccountId with a parameter accountId
       //Access modifier is void
       public void setAccountId(int accountId){
           
           //This operation is equal to accountId    
           this.accountId = accountId;
       }//End method
       
       //Create public method getProductId that returns an int value
       public int getProductId(){
           
           //Return productId
           return productId;
       }//End method
       
       //create public method setProductId with a parameter productId
       //Access modifier is void
       public void setProductId(int productId){
          
           //This operation is equal to productId
           this.productId = productId;
       }//End method
       
       //Override public method toString that returns a String value
       @Override
       public String toString(){
           
           //Initialize String variable accounts and set it equal to a string representation
           //of the values for accountId and productId
           return String.format("\n\nAccount Application Daily Report \n" + "Account ID: " + accountId + 
                   "\nProduct ID: " + productId);
           
                
       }//End method
       
       //Create public method computeSales
       //Access modifier is void
       public abstract void computeSales();
       
        
    }//end class
    
    
    
    
    //Create public class Paper that is a Subclass of Accounts 
    public class Paper extends Accounts{
        
        //Initialize private double variable poundsSold    
        private double poundsSold;
        //Initialize private double variable pricePerPound
        private double pricePerPound;
        
        //Create public method getPoundsSold that returns a double value
        public double getPoundsSold(){
            
            //return poundsSold
            return poundsSold;
        }//End method
        
        //Create public method setPoundSold with a parameter poundsSold
        //Access modifier is void
        public void setPoundsSold(double poundsSold){
           
            //This operation is equal to the value of poundsSold
            this.poundsSold = poundsSold;
        }//End method
        
        
        //Createe public method getPricePerPound that returns a double value
        public double getPricePerPound(){
            
            //Return pricePerPound
            return pricePerPound;
        }//End method
        
        //Create public method setPricePerPound with a parameter pricePerPound  
        //Access moedifier is void
        public void setPricePerPound(double pricePerPound){
            
            //This operation is equal to the value of pricePerPound
            this.pricePerPound = pricePerPound;
        }//End method
        
        @Override
        //Override public method toString that returns a String value
        public String toString(){
            
            //Initialize String variable paper and set it equal to a string representation
            //of the values for poundsSold and pricePerPound
            return String.format("Paper Sales: \n" + "Pounds of paper sold: " + poundsSold 
                    + "\nPrice per Pound: " + pricePerPound);
           
        }//End method
        
        public String toStringSuper(){
            
            return super.toString();
            
        }
        @Override
        //Override public method computeSales
        //Access modifier is void
        public void computeSales(){
           
            //Initialize double variable calculation and set it equal to 
            //this.poundsSold times this.pricePerPound
            double calculation = this.poundsSold * this.pricePerPound;
            //Print to the user a string representation of the value for calculation
            System.out.println("The current sales amount for paper products: " + 
                    calculation);
        }//End method
        
    }//End class
    
    
    //Create public class Supplies that is a subclass of Accounts
    public class Supplies extends Accounts{
        
        //Initialize private double variable totalSuppliesSold
        private double totalSuppliesSold;
        //Initlalize private double variable officeSupplies
        private double officeSupplies;
        //Initialize public double variable apparel
        private double apparel;
        //Initialize public double variable book
        private double book;
        
        //Create public method getOfficeSupplies that returns a double value
        public double getOfficeSupplies(){
            
            //Return officeSupplies
            return officeSupplies;
        }//End method
        
        //Create public method setOfficeSupplies with a parameter officeSupplies
        
        //Access modifier is void
        public void setOfficeSupplies(double officeSupplies){
            
            //This operation is equal to officeSupplies
            this.officeSupplies = officeSupplies;
        }//End method
        
        //Create public method getApparel that returns a double value
        public double getApparel(){
        
            //Return apparel
            return apparel;
        }///End method
        
        //Create public method setApparel with a parameter apparel
        //Access modifier is void
        public void setApparel(double apparel){
                    
            //This operation is equal to apparel
            this.apparel = apparel;
        }//End method
        
        //Create public method getBook that returns a double value
        public double getBook(){
            
            //Return book
            return book;
        }//End method
        
        //Create public method setBook with a parameter book
        //Access modifier is void
        public void setBook(double book){
            
            //This operation is equal to book
            this.book = book;
        }//End method
        
        //Create public method getTotalSuppliesSold that returns a double value
        public double getTotalSuppliesSold(){
            
            //Return totalSuppliesSold
            return totalSuppliesSold;
        }//End method
       
        @Override
        //Override public method computeSales
        //Access modifier is void
        public void computeSales(){
            
            //Initialize double variable calculation and set it equal to 
            //this.officeSupplies plus this.apparel plus this.book
            double calculation = this.officeSupplies + this.apparel + this.book;
            
            //Print to the user a string representation of the value for calculation
            System.out.println("The current sales amount for supply products: " +
                    calculation);  
            
            this.totalSuppliesSold = calculation;
        }//End method
        
        @Override
        //Override public method toString that returns a String value
            public String toString(){
               
            //Initialize String variable supplies and set it equal to a string representation
            //of the values for book, apparel, officeSupplies and totalSuppliesSold
            return String.format("Supply Sales: \n" + "Sales for books: " + book + "\nSales"
                    + " for apparel: " + apparel + "\nSales for office supplies: " + officeSupplies
                    + "\nThe current sales total for supplies: " 
                    + this.totalSuppliesSold);       
           
        }//End method
        
    }//End class
    Last edited by grimlek; 12-19-2012 at 05:38 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    Wow, that's a lot of code.

    Don't "highlight" in red, but instead use code tags around your code so that it retains its formatting and is easier to read. Consider showing us the pertinent code, not all the code. Please see my link below to see how to use code tags. Also, please tell us the details about what is or isn't working. Assume that we know nothing about your current program and its behavior.

  3. #3
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    ok Thank You fuberable, I am trying to use an array to loop through objects of subclasses to call the same method in each class called computeSales(). For some reason it is not working, I do not see any information when I run the program. When I call each method individually such as object1.computeSales(); is prints fine.


    Java Code:
    Accounts[] array = new Accounts [3];
     
    array[0] = new Paper();
     array[1] = new Services();
     array[2] = new Supplies();
     
    for(Accounts x: array){
     
    array1[x].computeSales();
     
    }
     
    
    Accounts[] array1 = new Accounts[3];
     
    array1[0] = new Paper();
    array1[1] = new Services();
     array1[2] = new Supplies();
     
    for(Accounts x: array1){
     
    array1[x].toString();
     
    }
    Last edited by grimlek; 12-19-2012 at 05:11 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    Again, please read my link below on how to use code tags, and edit both of your posts now. We are volunteer helpers here and appreciate it greatly when you put in the effort to make it easier to help you.
    Last edited by Fubarable; 12-19-2012 at 04:54 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    So nothing prints out at all when you call this method in your current program?

  6. #6
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    The toString array does not if I use the array

    the compute sales does not do any calculations and returns:

    wait im sry.

    Java Code:
    Sales Report: 
    The current sales amount for paper products: 0.0
    The current sales amount for services provided: 0.0
    The current sales amount for supply products: 0.0
    BUILD SUCCESSFUL (total time: 8 seconds)
    that is what is prints out. Instead it needs to do the correct calculations. It just returns 0.0 and the toString array does not print out anything.
    Last edited by grimlek; 12-19-2012 at 05:15 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    Based on your current code, why would you expect it to print a non-zero number?

    Note 1: your code needs a lot less comments in it as they detract from the code and actually make it harder to understand.
    Note 2: Watch out for "variable shadowing" where you re-declare a variable inside of a method or constructor. When you do that, you *think* that you may be setting a class field, but you're not.

  8. #8
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    because the user enters the values and then the values are returned using the scanner class

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    Quote Originally Posted by grimlek View Post
    because the user enters the values and then the values are returned using the scanner class
    Please show where the user enters values. Where is the Scanner class used to obtain information from the user?

  10. #10
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    Quote Originally Posted by Fubarable View Post
    Please show where the user enters values. Where is the Scanner class used to obtain information from the user?
    I am sorry forgot to include my main class...Issue is fixed without comments for easier viewing.

  11. #11
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    Quote Originally Posted by grimlek View Post
    I am sorry forgot to include my main class...Issue is fixed without comments for easier viewing.

    I also included the wrong for loop should be

    for (int x = 0; x < array.length; x++){

    }

    the current for loop gives an for data type. Variable x currently has a data type of accounts and should be an int. So the enhanced for loop wont work.

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Need Help looping through an Array

    You are using for-each loop in your code. In this case each element of the array is already extracted from the array. You can't access array using index with Accounts type like you did in array[x].

    So what you need to do is like the snippet below:

    Java Code:
    for (Accounts x : array) {
        x.computeSales().
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  14. #14
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    Quote Originally Posted by Fubarable View Post
    The code won't compile for me as it recognizes no Services class.
    blah....another mistake, this what happens when in a rush. I am sorry, the issue is fixed without comments under the main class.......

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    OK, now to an understanding of your problem: where do you create the objects that you place in your arrays? Please post the code where you create the new objects that go into the array.

  16. #16
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    so paper(); is calling a method and not an object.

    I have tried

    array1[0] = Paper1;
    array1[1] = Services1;
    array1[2] = Supplies1;

    but I got an error, those objects were created at the top of the main class. But for what im using for the array now I dont have any objects.

  17. #17
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    Java Code:
    Accounts Accounts1 =  new Paper();         
    Services Services1 = new Services();         
    Paper Paper1 = new Paper();         
    Supplies Supplies1 = new Supplies();

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help looping through an Array

    The code you posted originally showed that this is where you created the objects that were placed in the arrays:
    Java Code:
          Accounts[] array = new Accounts [3];
           
          array[0] = new Paper();
          array[1] = new Services();
          array[2] = new Supplies();
    Do you see why this is a problem, why none of these objects will reflect any input that was given by the user above?

  19. #19
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    yes I do understand now, the user input was stored within the objects of each class created before the array. The array created new objects which means that the information that the user input was not within the objects created by the array.

    So I need to use the objects created before the arrays.....
    Last edited by grimlek; 12-19-2012 at 06:00 AM.

  20. #20
    grimlek is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need Help looping through an Array

    I tried using the objects created before but It didnt work I receive the following error while using

    Java Code:
    array1[0] = Paper1;
    array1[1] = Services1;
    array1[2] = Supplies1;
    It says cannot find symbol and if I hit alt enter it says to create class paper1.....

Page 1 of 2 12 LastLast

Similar Threads

  1. Looping through an array
    By Person264 in forum New To Java
    Replies: 0
    Last Post: 12-12-2012, 10:59 PM
  2. Looping over averages in an int[][] array
    By adventurerOK in forum Advanced Java
    Replies: 1
    Last Post: 09-28-2011, 09:16 PM
  3. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  4. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  5. Looping Array Elements
    By enzyme in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •