Results 1 to 9 of 9
  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default How to substract each value from the initial value.

    I'm writing about a program that first asks how much the user would like to donate and then repeated asks for the name of a country and how much to donate to it. It keeps track of how much is left to be donated and stops when the amount left gets to 0.

    This is my program.

    Java Code:
    import javax.swing.*;
    
    class exercise5
    {
        public static void main (String[] param)
        { 
           donations();
            System.exit(0);
            
        } // END main
    
        public static void donations()
        {
           String again = "yes";
           String donate = JOptionPane.showInputDialog("How much would you like to donate?");
           int fulldonate = Integer.parseInt(donate);
           
           while (again.equals("yes"))
           {
              
              String country = JOptionPane.showInputDialog("Which country are you considering?");
              
              if (country != null)
             {
         
            
       
    			if (country.equalsIgnoreCase ("Haiti"))
     	        {
            	   JOptionPane.showMessageDialog(null, country + " is current in URGENT need of aid");
            	   String donate1 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid1 = Integer.parseInt(donate1);
            	   		int donleft1 = fulldonate - aid1;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft1 + "pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Switzerland"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is not in need of aid");
            	    String donate2 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid2 = Integer.parseInt(donate2);
            	   		int donleft2 = (fulldonate - donleft1) - aid2;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft2 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Pakistan"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGENT need of aid");
            	    String donate3 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid3 = Integer.parseInt(donate3);
            	   		int donleft3 = (fulldonate - donleft2) - aid3;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft3 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Chile"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in SOME need of aid");
            	    String donate4 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid4 = Integer.parseInt(donate4);
            	   		int donleft4 = (fulldonate - donleft3) - aid4;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft4 + " pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Indonesia"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGEENT need of aid");
            	    String donate5 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid5 = Integer.parseInt(donate5);
            	   		int donleft5 = (fulldonate - donleft4) - aid5;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft5 + " pounds left.");
            	}  
            	else if (country.equals (""))
            	{
            	    JOptionPane.showMessageDialog(null, "Error, please type in the country you would like to donate");
            	}
            	else
            	{
            	    JOptionPane.showMessageDialog(null, "This country does not need aid");
            	}  
            	
            	
              
              again = JOptionPane.showInputDialog("Continue? (yes/no)");
            }
            
         
          }
           
    
    
        }// END donations
        
    }// END class exercise5
    I got this error

    Java Code:
    exercise5.java:52: cannot find symbol
    symbol  : variable donleft1
    location: class exercise5
            	   		int donleft2 = (fulldonate - donleft1) - aid2;
            	   		                                      ^
    exercise5.java:60: cannot find symbol
    symbol  : variable donleft2
    location: class exercise5
            	   		int donleft3 = (fulldonate - donleft2) - aid3;
            	   		                                     ^
    exercise5.java:68: cannot find symbol
    symbol  : variable donleft3
    location: class exercise5
            	   		int donleft4 = (fulldonate - donleft3) - aid4;
            	   		                                     ^
    exercise5.java:76: cannot find symbol
    symbol  : variable donleft4
    location: class exercise5
            	   		int donleft5 = (fulldonate - donleft4) - aid5;
            	   		                                     ^
    4 errors

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    donleft1, donleft2, donleft3 and donleft4 have gone out of scope when you try to reuse them. In other words they are local to the if statements you declared them in and cannot be used outside of said if statement

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

    Default

    Any variable declared within a block is visible only within that block. So if you declare donLeft1 inside an if block, it is only visible in that if block and is not visible beyond that block. A solution is to declare the variables earlier in the method or in the class.

  4. #4
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I tried to bring them out. but I'm still confused how I do link them to the value inside the if statements

    Java Code:
    import javax.swing.*;
    
    class exercise5
    {
        public static void main (String[] param)
        { 
           donations();
            System.exit(0);
            
        } // END main
    
    
        public static void donations()
        {
           String again = "yes";
           String donate = JOptionPane.showInputDialog("How much would you like to donate?");
           int fulldonate = Integer.parseInt(donate);
           
           while (again.equals("yes"))
           {
              int donleft1 = fulldonate - aid1;
           	  int donleft2 = (fulldonate - donleft1) - aid2;
           	  int donleft3 = (fulldonate - donleft2) - aid3;
           	  int donleft4 = (fulldonate - donleft3) - aid4;
           	  int donleft5 = (fulldonate - donleft4) - aid5;
           	
           	  
           	 
           	 
           	  
              String country = JOptionPane.showInputDialog("Which country are you considering?");
              
              if (country != null)
             {
         
            
       
    			if (country.equalsIgnoreCase ("Haiti"))
     	        {
            	   JOptionPane.showMessageDialog(null, country + " is current in URGENT need of aid");
            	   String donate1 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		  int aid1 = Integer.parseInt(donate1);
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft1 + "pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Switzerland"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is not in need of aid");
            	    String donate2 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid2 = Integer.parseInt(donate2);
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft2 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Pakistan"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGENT need of aid");
            	    String donate3 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		 int aid3 = Integer.parseInt(donate3);
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft3 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Chile"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in SOME need of aid");
            	    String donate4 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		 int aid4 = Integer.parseInt(donate4);
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft4 + " pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Indonesia"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGEENT need of aid");
            	    String donate5 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		int aid5 = Integer.parseInt(donate5);
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft5 + " pounds left.");
            	}  
            	else if (country.equals (""))
            	{
            	    JOptionPane.showMessageDialog(null, "Error, please type in the country you would like to donate");
            	}
            	else
            	{
            	    JOptionPane.showMessageDialog(null, "This country does not need aid");
            	}  
            	
            	
              
              again = JOptionPane.showInputDialog("Continue? (yes/no)");
            }
            
         
          }
           
    
    
        }// END donations
        
    }// END class exercise5
    Java Code:
    exercise5.java:31: cannot find symbol
    symbol  : variable aid1
    location: class exercise5
              int donleft1 = fulldonate - aid1;
                                          ^
    exercise5.java:32: cannot find symbol
    symbol  : variable aid2
    location: class exercise5
           	  int donleft2 = (fulldonate - donleft1) - aid2;
           	                                           ^
    exercise5.java:33: cannot find symbol
    symbol  : variable aid3
    location: class exercise5
           	  int donleft3 = (fulldonate - donleft2) - aid3;
           	                                           ^
    exercise5.java:34: cannot find symbol
    symbol  : variable aid4
    location: class exercise5
           	  int donleft4 = (fulldonate - donleft3) - aid4;
           	                                           ^
    exercise5.java:35: cannot find symbol
    symbol  : variable aid5
    location: class exercise5
           	  int donleft5 = (fulldonate - donleft4) - aid5;
           	                                           ^
    5 errors

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Simply declare donleft's and aid's etc where you have but do not assign them anything yet or any calculations. Then use those variables where you did the first time but do not put a type in front of them i.e. int

    You are getting errors now as you are trying to use aid's that have yet to be declared.

  6. #6
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I did as what you told me
    Java Code:
    import javax.swing.*;
    
    class exercise5
    {
        public static void main (String[] param)
        { 
           donations();
            System.exit(0);
            
        } // END main
    
    
    
        public static void donations()
        {
           String again = "yes";
           String donate = JOptionPane.showInputDialog("How much would you like to donate?");
           int fulldonate = Integer.parseInt(donate);
           int donleft1,donleft2,donleft3,donleft4,donleft5;
           int aid1,aid2,aid3,aid4,aid5;
           
           while (again.equals("yes"))
           {
              
         
           	  
              String country = JOptionPane.showInputDialog("Which country are you considering?");
              
              if (country != null)
             {
         
            
       
    			if (country.equalsIgnoreCase ("Haiti"))
     	        {
            	   JOptionPane.showMessageDialog(null, country + " is current in URGENT need of aid");
            	   String donate1 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		aid1 = Integer.parseInt(donate1);
            	   	donleft1 = fulldonate - aid1;
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft1 + "pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Switzerland"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is not in need of aid");
            	    String donate2 = JOptionPane.showInputDialog("How much would you like to donate to Switzerland?");
            	   		    aid2 = Integer.parseInt(donate2);
            	   		donleft2 = (fulldonate - donleft1) - aid2;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft2 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Pakistan"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGENT need of aid");
            	    String donate3 = JOptionPane.showInputDialog("How much would you like to donate to Pakistan?");
            	   		    aid3 = Integer.parseInt(donate3);
            	   		donleft3 = (fulldonate - donleft2) - aid3;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft3 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Chile"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in SOME need of aid");
            	    String donate4 = JOptionPane.showInputDialog("How much would you like to donate to Chile?");
            	   		    aid4 = Integer.parseInt(donate4);
            	   		donleft4 = (fulldonate - donleft3) - aid4;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft4 + " pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Indonesia"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGEENT need of aid");
            	    String donate5 = JOptionPane.showInputDialog("How much would you like to donate to Indonesia?");
            	   		   aid5 = Integer.parseInt(donate5);
            	   		donleft5 = (fulldonate - donleft4) - aid5;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft5 + " pounds left.");
            	}  
            	else if (country.equals (""))
            	{
            	    JOptionPane.showMessageDialog(null, "Error, please type in the country you would like to donate");
            	}
            	else
            	{
            	    JOptionPane.showMessageDialog(null, "This country does not need aid");
            	}  
            	
            	
              
              again = JOptionPane.showInputDialog("Continue? (yes/no)");
            }
            
         
          }
           
    
    
        }// END donations
        
    }// END class exercise5
    How do I solve this error?

    Java Code:
    exercise5.java:57: variable donleft1 might not have been initialized
            	   		donleft2 = (fulldonate - donleft1) - aid2;
            	   		                                ^
    exercise5.java:65: variable donleft2 might not have been initialized
            	   		donleft3 = (fulldonate - donleft2) - aid3;
            	   		                                ^
    exercise5.java:73: variable donleft3 might not have been initialized
            	   		donleft4 = (fulldonate - donleft3) - aid4;
            	   		                                ^
    exercise5.java:81: variable donleft4 might not have been initialized
            	   		donleft5 = (fulldonate - donleft4) - aid5;
            	   		                                ^
    4 errors

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    yeah just initialise all your donleft's to zero :o

    You could just have the fulldonate variable only
    and subtract each aid from it everytime you make a donation
    Java Code:
    fulldonate-=aid1;
    Java Code:
    fulldonate-=aid2;
    etc

  8. #8
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    THANKS A LOT. It worked
    It was working perfectly until now.
    everytime I type in the country it comes out like this.
    Java Code:
    import javax.swing.*;
    
    class exercise5
    {
        public static void main (String[] param)
        { 
           donations();
            System.exit(0);
            
        } // END main
    
    
    
        public static void donations()
        {
           String again = "yes";
           String donate = JOptionPane.showInputDialog("How much would you like to donate?");
           int fulldonate = Integer.parseInt(donate);
           int donleft1 = 0,donleft2 = 0,donleft3 = 0,donleft4 = 0;
           int aid1,aid2,aid3,aid4;
           
           while (again.equals("yes"))
           {
              
         
           	  
              String country = JOptionPane.showInputDialog("Which country are you considering?");
              
              if (country != null)
             {
         
            
       
    			if (country.equalsIgnoreCase ("Haiti"))
     	        {
            	   JOptionPane.showMessageDialog(null, country + " is current in URGENT need of aid");
            	   String donate1 = JOptionPane.showInputDialog("How much would you like to donate to Haiti?");
            	   		aid1 = Integer.parseInt(donate1);
            	   	donleft1 = fulldonate - aid1;
            	   		
            	   JOptionPane.showMessageDialog(null, "You have" + donleft1 + "pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Switzerland"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is not in need of aid");
            	    
            	}   
            	else if (country.equalsIgnoreCase ("Pakistan"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGENT need of aid");
            	    String donate2 = JOptionPane.showInputDialog("How much would you like to donate to Pakistan?");
            	   		    aid2 = Integer.parseInt(donate2);
            	   		donleft2 = (fulldonate - donleft1) - aid2;
            	   JOptionPane.showMessageDialog(null, "You have " + donleft2 + " pounds left.");
            	}   
            	else if (country.equalsIgnoreCase ("Chile"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in SOME need of aid");
            	    String donate3 = JOptionPane.showInputDialog("How much would you like to donate to Chile?");
            	   		    aid3 = Integer.parseInt(donate3);
            	   		donleft3 = (fulldonate - donleft2) - aid3;
            	   JOptionPane.showMessageDialog(null, "You have " + donleft3 + " pounds left.");
            	}
            	else if (country.equalsIgnoreCase ("Indonesia"))
            	{
            	    JOptionPane.showMessageDialog(null, country + " is currently in URGEENT need of aid");
            	    String donate4 = JOptionPane.showInputDialog("How much would you like to donate to Indonesia?");
            	   		   aid4 = Integer.parseInt(donate4);
            	   		donleft4 = (fulldonate - donleft3) - aid4;
            	   JOptionPane.showMessageDialog(null, "You have" + donleft4 + " pounds left.");
            	}  
            	else if (country.equals (""))
            	{
            	    JOptionPane.showMessageDialog(null, "Error, please type in the country you would like to donate");
            	}
            	else
            	{
            	    JOptionPane.showMessageDialog(null, "This country does not need aid");
            	}  
            	
            	
              
              again = JOptionPane.showInputDialog("Continue? (yes/no)");
            }
            
         
          }
           
    
    
        }// END donations
        
    }// END class exercise5

    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Haiti"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    	at java.lang.Integer.parseInt(Integer.java:449)
    	at java.lang.Integer.parseInt(Integer.java:499)
    	at exercise5.donations(exercise5.java:27)
    	at exercise5.main(exercise5.java:14)

  9. #9
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

Similar Threads

  1. tricky initial value problem
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 10:02 AM
  2. Initial capacity of ArrayList
    By bugger in forum New To Java
    Replies: 12
    Last Post: 12-30-2009, 09:16 AM
  3. How to set the designer initial properties when making web report
    By freezea in forum Reviews / Advertising
    Replies: 0
    Last Post: 06-04-2009, 05:25 PM
  4. Area Calculation: Add, Substract, XOR
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:08 AM
  5. Replies: 1
    Last Post: 08-13-2007, 04:46 AM

Posting Permissions

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