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 11 of 11

Thread: Very basic help needed

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very basic help needed

    As you are about to find out, I'm just picking up programming and I have a book in which I'm trying to complete a problem. I believe I'm having scope problems which I don't understand any assistance would be greatly appreciated. I'm trying to take in entered data which is self explanatory if you read the dialogues. But I get the input, and i cant seem to use the p1area and p2area variables outside of the if-statements. I want to use them outside because I need to use them in a final output which summarizes all the data entered. I don't know if any of what I'm typing is enough to get assistance, but if you think you can see anything which is going wrong, please don't hesitate to respond. Thank you.



    import javax.swing.JOptionPane;
     
    public class CopyOfPizzas {
     
     
    	public static void main(String[] args) {
     
    	double p1area;
    	double p2area;
    	String input1 = null;
    	String input2 = null;
    	String shape = null;
    	String shape2 = null;
     
    	input1 = JOptionPane.showInputDialog("shape of pizza, R or C (Q to quit)");
    	if(input1 == null || input1.equals("q") || input1.equals("Q")) {
    			return;
    		}
     
    	shape = input1;
     
    			if (shape.equals("r") || shape.equals("R")) {
    				p1area = calc_rectangle();
    			}
    			if (shape.equals("c") || shape.equals("C")) {
    			p1area = calc_circle();
    			}
    		R or C is input earlier and then the other input is retrieved when calc_rectangle/calc_circle is called and it 
     
    returns a value and assigned to p1area...once i get that assigned back in the if-statement above this comment i want to 
     
    use p1area outside of the if-statement in a JOptionPane.showMessageDialog() with some other details.  When i try to use 
     
    it outside of the if-statement it says the local variable may not have been initialized
     
     
    	input2 = JOptionPane.showInputDialog("shape of pizza, R or C (Q to quit)");
    	if(input2 == null || input2.equals("q") || input2.equals("Q")) {
    			return;
    		}
    			shape2 = input2;
     
    			if (shape2.equals("r") || shape2.equals("R")) {
    				p2area = calc_rectangle();
    			}
    			if (shape2.equals("c") || shape2.equals("C")) {
    				p2area = calc_circle();
    			}
    	JOptionPane.showMessageDialog(null, "Pizza one area: " + p1area + "\nPizza two area: " + p2area);
     
    	}
     
     
    	public static double calc_rectangle() {
    		String temp1 = JOptionPane.showInputDialog("length in inches: ");
    		String temp2 = JOptionPane.showInputDialog("width in inches: ");
    		Double length = Double.parseDouble(temp1);
    		Double width = Double.parseDouble(temp2);
     
    		double area = length * width;
    			return area;
    	}
     
    	public static double calc_circle() {
    		String temp1 =                      JOptionPane.showInputDialog("diameter in inches");
    		double diameter = Double.parseDouble(temp1);
     
    		double area = (3.14 * (Math.pow(diameter/2, 2)));
    		return area;
    	}
     
    }
    Last edited by goochasauras; September 26th, 2012 at 10:44 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Very basic help needed

    Can you show where you are trying to use the variable that is not working?
    Also please use [code=java][/code] so the code gets a colorful highlight for my old eyes.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic help needed

    i edited the statement and threw in a big wad of text, please if read through

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Very basic help needed

    Quote Originally Posted by goochasauras View Post
    i edited the statement and threw in a big wad of text, please if read through
    Reading it now, as a side note, see this link as I read your "big wad of text".

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Very basic help needed

    Quote Originally Posted by goochasauras View Post
    R or C is input earlier and then the other input is retrieved when calc_rectangle/calc_circle is called and it

    returns a value and assigned to p1area...once i get that assigned back in the if-statement above this comment i want to

    use p1area outside of the if-statement in a JOptionPane.showMessageDialog() with some other details. When i try to use

    it outside of the if-statement it says the local variable may not have been initialized
    If I understand your text, all you have to do is pass that value to a method. This link may help.

    If not post the code where you are trying to use the variable and getting the error message.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic help needed

    I added another line of code as an example of how I want to use it...it's at the bottom of Main()...but i get red squigglies saying the local variable may not have been initialized

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Very basic help needed

    If you look at every possible path through the code, when you get to that point there is a chance a variable was not initialized.

    A scenario the compiler is complaining about.
    What happens if the input is the String "8"?
    (!= shorthanded for .equals)
    8 != r
    8 != R
    8 != c
    8 != C
    p1area p2area have not been initialized and can not be used.

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic help needed

    but i did create the variables at the beginning of main() and in the if statements i called functions and assigned the returned value to the variables..but as soon as i try to use those variables outside of the if statements i get an error

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Very basic help needed

    You declared the variables.
    The complaint is that they were not initialized. (A fancy way to say "given a value")

    Every possible path through your code that leads up to the use of any variable (p1area) must include a line of code that gives a value to the variable. (somewhere through the path has to say p1area = 24 or some valid value)

    As it stands if a bad value, like the String "8" or the String "fred", was returned as the value for the showInputDialog call, p1area and p2area will not be given a value.
    Your code has no way to know if showInputDialog will return one of r, R, c, C or not, so the compiler can see there is a way to read a variable you have not given a value yet.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic help needed

    so what would be a good alternative to how I'm proceeding through this?

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Very basic help needed

    You have to make sure that every possible path through the code will initialize the variables before you use them.
    When and where you initialize them depends on what you need. The value you give depends also on what you need.
    I would start early, like where I declare my variables, I usually try to initialize them with a value.

    Can you visualize different possible paths through the code? If (condition) this else that... That makes two possible paths to go down. You just have to make sure the variables that are used were forcefully set to a value before you try to read the value, no matter which path was taken.

Similar Threads

  1. Need Help, Not sure how to fix basic error!
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2012, 01:41 PM
  2. Basic
    By deemu in forum Java Theory & Questions
    Replies: 1
    Last Post: March 5th, 2012, 08:36 AM
  3. A basic calculator
    By SathApoc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 22nd, 2011, 11:47 AM
  4. Need basic String help
    By Sadalmelik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2011, 06:05 AM
  5. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM

Tags for this Thread