Results 1 to 4 of 4
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default if else inside while loop??

    Hi guys. Im having trouble with passing my variables when they are inside a while loop.

    Basically what i am trying to do is get user input: a character either P, C, M or X and a Int. If P C or M is input the int's are taken and tallied up. The program runs tallying up until an input of X is given, which will exit the loop and print the results of P C and M.

    The problem seems to be that I cant create a object from within a ie else????

    Basically what happens is it just keeps looping asking me to input an amount(int).

    This one has been bugging me for a couple of hours now so help is appreciated...

    Thanks guys. I hope you understand what i am asking.

    Java Code:
    public class Category {
    		 String category;
    		 int amount;
    		
    		int C = 0;
    		int M = 0;
    		int P = 0;
    		
    		public Category(String category, int amount)
    		{
    			this.category = category;
    			this.amount = amount;
    		}
    		public int tally()
    		{
    
    			if(this.category.equals("P"))
    			{
    				this.P = this.P + this.amount;
    			}
    			else if(this.category.equals("C"))
    			{
    				this.C = this.C + this.amount;
    			}
    			else if(this.category.equals("M"))
    			{
    				this.M = this.M + this.amount;
    			}
    			return this.P;
    		}
    		public int getC()
    		{
    			return this.C;
    		}
    		public int getM()
    		{
    			return this.M;
    		}
    		public int getP()
    		{
    			return this.P;
    		}
    }

    TESTER

    Java Code:
    import java.util.Scanner;
    public class CategoryTester {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		
    		System.out.println("Please enter the category e.g. P, C, M or X to exit");
    			String category = scanner.next();
    			System.out.println("You entered as a cat - " + category);
    			
    		int i = 1;
    		while(i <= 3)
    	{	
    			if(category.equals("P"))
    		{
    			System.out.println("Please enter the amount");
    			int amount = scanner.nextInt();
    			Category category1 = new Category("P", amount);
    			i = 1;
    		}
    		else if (category.equals("C"))
    		{
    			System.out.println("Please enter the amount");
    			int amount = scanner.nextInt();
    			Category category1 = new Category("C", amount);
    			i = 1;
    		}
    		else if (category.equals("M"))
    		{
    			System.out.println("Please enter the amount");
    			int amount = scanner.nextInt();
    			Category category1 = new Category("M", amount);
    			i = 1;
    		}
    		else if (category.equals("X"))
    		{
    			System.out.println("Printers = " +
    					category1.getP() + 
    					"\nComputers = " +
    					category1.getC() +
    					"\nMedia = " +
    					category1.getM());
    			i = 3;
    		}
    	}
    }
    
    }
    Last edited by jonytek; 02-17-2011 at 09:29 AM.

  2. #2
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default Reply of if else inside while loop

    I think The problem is because you are creating a new object in every iteration of while loop so create an object outside of while loop and inside call the setter methods of that object...

  3. #3
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Sorry but I dont fully understand how I would do that.

  4. #4
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 02-13-2011, 01:03 PM
  2. try and catch inside a for loop fails!!!
    By Kingmaha64 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 03:40 AM
  3. Loop inside a switch
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 04:25 AM
  4. Problem printing inside FOR loop
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 06:02 PM
  5. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 02:19 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
  •