Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: ArrayList question

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList question

    I am new to programming and new to posting on this forum so please forgive me if I make any mistakes.
    I have the following code:
    public class BillsDriver 
    {
     
     
    	public static void main(String[] args) 
    	{
    		//array of Bills
    		ArrayList<Bills> bills = new ArrayList<Bills>();
     
    		ArrayList<Income> income1 = new ArrayList<Income>();
     
    		boolean money = true;
    		while(money)
    		{
     
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter income: ");
    		double incomes = scan.nextDouble();
     
    		Income thisIncome = new Income(incomes);
    		income1.add(thisIncome);
     
    		System.out.println("More income?(true/false)");
    		money = scan.nextBoolean();
    		}
     
    		printIncome(income1);
     
     
    		boolean more = true;
     
    		while(more)
    		{
    			Scanner sc = new Scanner(System.in);
    			System.out.println("Enter bill name: ");
    			String name = sc.nextLine();
     
    			System.out.println("Enter bill amount: ");
    			double amount = sc.nextDouble();
     
    			Bills thisBill = new Bills(name, amount);
     
    			bills.add(thisBill);
     
    			System.out.println("More bills?(true/false)");
    			more = sc.nextBoolean();
     
    		}
     
    		printBill(bills);
     
     
    	}
     
    	public static void printIncome(ArrayList<Income> income) 
    	{
    		double totals =0;
    		for(int i=0; i<income.size(); i++)
    		{
    			System.out.println("Income total: " + i + income.get(i).toString());
     
    			totals += ((Income)income.get(i)).getTotals();
    			System.out.println("\tTotal income: \t" + totals);
    		}
     
    	}
     
    	public static void printBill(ArrayList<Bills> bills) 
    	{
    		//prints out data
    		double total = 0;
    		for(int i = 0; i < bills.size(); i++)
    		{
    			//show ArrayList of bills added by user
    			System.out.println("Bill number: " + (i+1) + ":" + bills.get(i).toString());
     
     
    			//get total amount due
    			total += ((Bills) bills.get(i)).getTotal();
    			System.out.println("\tTotal amount due: \t" + total);
     
    		}
     
    	}
     
    }

    What I have questions about is how to subtract the total of the Bills ArrayList from the total of the Income ArrayList?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList question

    subtract the total of the Bills ArrayList from the total of the Income ArrayList?
    Is this what you are asking:
    result = totalOfIncome - totalOfBills;
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    Yes.
    I have tried adding:
    double result = 0;
    result = totals - total;
    System.out.println("Results: " +result);
    But I kept getting different errors to create a totals variable and a total variable.
    If possible I just wanted to call the totals and total amounts that are already established and subtract totals - total.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList question

    I kept getting different errors
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Can the definitions of the methods that compute the totals be changed to have the methods return the totals that they compute?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    public class BillsDriver 
    {
     
     
    	public static void main(String[] args) 
    	{
    		//arrayList of Bills Class
    		ArrayList<Bills> bills = new ArrayList<Bills>();
     
    		//ArrayList of Income class
    		ArrayList<Income> income1 = new ArrayList<Income>();
     
    		boolean money = true;
    		while(money)
    		{
    		//get user input for income
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter income: ");
    		double incomes = scan.nextDouble();
     
    		Income thisIncome = new Income(incomes);
    		income1.add(thisIncome);
     
    		System.out.println("More income?(true/false)");
    		money = scan.nextBoolean();
    		}
     
    		printIncome(income1);
     
     
    		boolean more = true;
     
    		while(more)
    		{
    			//user input for name of bill and amount 
    			Scanner sc = new Scanner(System.in);
    			System.out.println("Enter bill name: ");
    			String name = sc.nextLine();
     
    			System.out.println("Enter bill amount: ");
    			double amount = sc.nextDouble();
     
    			Bills thisBill = new Bills(name, amount);
     
    			bills.add(thisBill);
     
    			System.out.println("More bills?(true/false)");
    			more = sc.nextBoolean();
     
    		}
     
    		printBill(bills);
     
     
    	}
     
    	private static void printIncome(ArrayList<Income> income) 
    	{
    		//prints income data
    		double totals =0;
    		for(int i=0; i<income.size(); i++)
    		{
    			//show ArrayList of Income
    			System.out.println("Income total: " + i + income.get(i).toString());
     
    			//calculates total income
    			totals += ((Income)income.get(i)).getTotals();
    			System.out.println("\tTotal income: \t" + totals);
    		}
     
    	}
     
    	private static void printBill(ArrayList<Bills> bills) 
    	{
    		//prints bills data
    		double total = 0;
    		for(int i = 0; i < bills.size(); i++)
    		{
    			//show ArrayList of bills added by user
    			System.out.println("Bill number: " + (i+1)+ " " + bills.get(i).toString());
     
     
    			//get total amount due
    			total += ((Bills) bills.get(i)).getTotal();
    			System.out.println("\tTotal amount due: \t" + total);
     
    		}
     
    	}
    	 public static void grandTotal()
    	 {
     
    		double grandTotal = 0;
    		grandTotal = totals - total;
    	 System.out.println("Results: " + grandTotal);
    	 }
     
     
     
    }

    The errors are on totals and total within grandTotal:
    Multiple markers at this line
    - totals cannot be resolved
    - total cannot be resolved

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList question

    Are those two variables defined in scope with where they are being used? In scope means defined within the same {}s as where used.
    class A_Class {
       int classVar; // define at class level;  in Scope for all methods
       void meth1() {
           int aVar;    // define aVar; in Scope only within meth1
           //  classVar in Scope here
    		 //...
       } // end meth1()
       void meth2() {
    	//  aVar in NOT in SCOPE here
           //  classVar in Scope here
           //...
        } //  end meth2()
    } // end class
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    No they are defined in different methods.
    totals is defined in private static void printIncome(ArrayList<Income> income)
    and
    total is defined in private static void printBills(ArrayList<Bills> bills)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList question

    variables defined in a method are only known inside that method. If you want several methods to be able to access them, define them at the class level as instance variables.

    A better solution is to have the method that computes a total, return it to the caller.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java arraylist question
    By maple1100 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2013, 10:09 PM
  2. ArrayList and foreach question
    By Egde in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2012, 06:18 PM
  3. Question about ArrayList
    By Jocko in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 07:54 PM
  4. ArrayList question
    By steel55677 in forum Collections and Generics
    Replies: 8
    Last Post: February 25th, 2012, 11:42 AM
  5. Please help ArrayList Question
    By SandeeBee in forum Collections and Generics
    Replies: 14
    Last Post: November 15th, 2011, 12:01 AM