Results 1 to 6 of 6
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Why does it say the variable input is not initialized?

    Java Code:
    import java.util.Scanner;
    public class fractioncalcs {
    	public static void main(String[] args) {
    		//initialization of variables begins here
    		double fraction1;
    		double fraction2;
    		boolean repeat = true; 
    		//scanner object for keyboard input
    		Scanner keyboard = new Scanner(System.in);
    while (repeat) {
    	String input;
    	if ("quit".equals(input))
    		repeat = true; //continues the loops
    	else
    		repeat = false;//this breaks the infinite while loop
    	System.out.println("Enter a expression (or \"quit\")");
    What I am trying to do is get the while loop to quit when the user inputs "quit".
    Isn't the variable already initialized?

    What I am trying to do with this code is create an input loop that quits correctly and echos the parts of the input, namely, fractions. The program must store the fractions internally as something other than a string before echoing.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default Re: Why does it say the variable input is not initialized?

    The compile does not see where is the variable input assigned a value?
    Something like: input = AVALUEHERE;

    Did you leave out the statement that uses one of the Scanner class's methods to read a String into input?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Why does it say the variable input is not initialized?

    Java Code:
       
    String input = null 
    if ("quit".equals(input))
            repeat = true; //continues the loops
        else
            repeat = false;//this breaks the infinite while loop
    So I attempted to fix the area by setting the variable input to null, but now the program is constantly terminating and I can't input anything.
    I'm not sure how to fix this.

  4. #4
    ShedoSurashu is offline Member
    Join Date
    Nov 2013
    Posts
    13
    Rep Power
    0

    Default Re: Why does it say the variable input is not initialized?

    I'm not at all experienced in Java but what do you use to set input? On your first post, it only shows you declaring String input but not putting anything into it.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default Re: Why does it say the variable input is not initialized?

    Step through the code one instruction at a time. Use a pencil and paper to write the the value of variables so you can keep track of there value as you move from one statement to the next.
    What is the value of input at line 2? What will the if statement do with that value?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Why does it say the variable input is not initialized?

    I think I figured out the problem. Thanks.

Similar Threads

  1. variable may not have been initialized...
    By ajetrumpet in forum New To Java
    Replies: 6
    Last Post: 03-24-2013, 11:35 AM
  2. Variable to initialized
    By Bungie in forum New To Java
    Replies: 10
    Last Post: 11-28-2012, 09:38 AM
  3. Variable may not have been initialized
    By gerchan in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 02:17 PM
  4. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 09:28 AM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:02 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
  •