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

Thread: Initializing and Receiving Variables, Creating Methods

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Initializing and Receiving Variables, Creating Methods

    For an online class I'm taking, we are creating an interactive version of the "99 Bottles of Beer" song. The user is supposed to be able to enter a quantity, container, and substance, and then those are supposed to be used in creating the song, so that it displays from "3 bags of chips..." to "2 bags of chips...", etc. I've had a couple hiccups in terms of variables, as my Eclipse IDE insist they haven't been initialized... can anyone help me?

    Oh yeah, code:

    package lab07;


    import java.util.Scanner;


    public class ConsumptionSongConsoleDriver {
    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("\tquantity: ");
    int quantity = keyboard.nextInt();

    System.out.print("\tcontainer: ");
    String container = keyboard.nextLine();
    keyboard.nextLine();

    System.out.print("\tsubstance: ");
    String substance = keyboard.nextLine();

    System.out.println(makeSong() );

    }

    public static String makeSong() {
    int quantity;
    String container, substance;
    String result = "";

    for(int count; quantity >= 0; quantity--) {
    result = count + "" + container + "s of " + substance + "on the wall," + count + container + "s of " + substance + "." + "Take one down, pass it around." + (count - 1) + "" + container + "s " + "of " + substance + "on the wall.\n";

    }

    return result;
    }

    }

    (Please, when giving solutions, keep it simple; the assignment is supposed to be this way, and we aren't that advanced yet.)


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Initializing and Receiving Variables, Creating Methods

    I've had a couple hiccups in terms of variables, as my Eclipse IDE insist they haven't been initialized.
    With compiler messages its always a good idea to post the message exactly as you see it. And indicate which line of your code it is referring to.

    -----

    As far as the need to initialise variables is concerned, this is just common sense but it is something we are apt to miss as we struggle with trying to do something unfamiliar as writing code is to begin with.

    "How long is a piece of string?"

    The point is that we cannot answer this unless we know *which* piece of string is being talked about. Variables and expressions like "a piece of string" have to have actual values before we can ask meaningful questions about them or do anything else with them. With variables it means they have to be given a value (be initialised) before you can do anything with them.

    Read through your code (or be guided by the compiler messages) and try to find the places where you try to use a variable, but have not yet given it a value. One thing to bear in mind is that variables declared in different methods have nothing to do with one another: they are different variables. This is true generally of variables declared in different {...} blocks.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Initializing and Receiving Variables, Creating Methods

    In your for loop, "count" & "quantity" is not initialized.

Similar Threads

  1. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  2. Initializing Variables
    By Tjstretch in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: October 29th, 2011, 12:37 PM
  3. Creating Methods for Input/Output Files
    By PapaGL0VE in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 17th, 2010, 09:07 AM
  4. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 06:10 PM
  5. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM

Tags for this Thread