Results 1 to 11 of 11
  1. #1
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [SOLVED] Constructor problem

    I'm trying out a simple thing. want to get a string from the user and a number, if the number is 1, I'll output the string itself, if the number is 2 i'll reverse the string. Here's my string:

    public Reverser(String in, int direction) {
    switch (direction) {
    case 1: normal = in;
    case 2: rev = in;
    }
    this is my Main class:
    class ReverseApp
    {
    public static void main(String[] args)
    {
    String normal,rev, output;
    int dir;

    while(true)
    {
    System.out.print("Enter your data: ");
    Reverser theReverser = new Reverser(normal,dir);

    if (dir == 2){

    output = theReverser.doRev();
    System.out.println("Reversed: " + output);
    }
    if (dir == 1){
    System.out.println("you have chosen to see what you have entered: "+ normal );
    }
    } // end while
    } // end main()

    I get the error: variable "normal" might not have been initialized!
    I am using the code of my book and just changed the constructor to play around with it!!
    can someone tell me what is wrong plz?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default huh?

    Are you sure you copied that from a book? Is it possible that it's an exercise that you have to fill in the missing parts. Maybe it's because it's late at night, but I don't see any user input and variables normal and dir are never assigned any values. I think you need to check that example from the book again.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    The code written here has lot of mistakes.
    For example ,
    switch and case should have break statement.Otherwise it would not work.
    Mak
    (Living @ Virtual World)

  4. #4
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Chris,
    thanks for your reply.
    yes the code is from data structures book. I didn't paste the whole thing though!

  5. #5
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Mak,
    what are the other mistakes other than the break in the switch?

    thanks in advance :)

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default constructor?

    I am using the code of my book and just changed the constructor to play around with it!!
    If you changed the constructor, either you didn't include that change in the above code or you erased it, because the above code doesn't have a constructor.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Chris,
    Reverser is the constructor.
    here is the complete code:
    class Reverser
    {
    private String normal,rev; // input string
    private String output; // output string
    //--------------------------------------------------------------
    public Reverser(String in, int direction) { // constructor

    switch (direction) {
    case 1: normal = in;
    case 2: rev = in;

    }
    }
    //--------------------------------------------------------------
    public String doRev() // reverse the string
    {
    int stackSize = normal.length(); // get max stack size
    StackX theStack = new StackX(stackSize); // make stack

    for(int j=0; j<normal.length(); j++)
    {
    char ch = normal.charAt(j); // get a char from input
    theStack.push(ch); // push it
    }
    output = "";
    while( !theStack.isEmpty() )
    {
    char ch = theStack.pop(); // pop a char,
    output = output + ch; // append to output
    }
    return output;
    } // end doRev()
    //--------------------------------------------------------------
    } // end class Reverser
    ////////////////////////////////////////////////////////////////
    class ReverseApp
    {
    public static void main(String[] args) throws IOException
    {
    String normal,rev, output;
    int dir;

    while(true)
    {
    System.out.print("Enter your : ");
    Reverser theReverser = new Reverser(normal,dir);
    //System.out.flush();
    //input = getString(); // read a string from kbd
    if (dir == 2){
    // make a Reverser

    output = theReverser.doRev(); // use it
    System.out.println("Reversed: " + output);
    }
    if (dir == 1){
    System.out.println("you have chosen to see what you have entered: "+ normal );
    }
    } // end while
    } // end main()
    class StackX
    {
    private int maxSize;
    private char[] stackArray;
    private int top;
    //--------------------------------------------------------------
    public StackX(int max) // constructor
    {
    maxSize = max;
    stackArray = new char[maxSize];
    top = -1;
    }
    //--------------------------------------------------------------
    public void push(char j) // put item on top of stack
    {
    stackArray[++top] = j;
    }
    //--------------------------------------------------------------
    public char pop() // take item from top of stack
    {
    return stackArray[top--];
    }
    //--------------------------------------------------------------
    public char peek() // peek at top of stack
    {
    return stackArray[top];
    }
    //--------------------------------------------------------------
    public boolean isEmpty() // true if stack is empty
    {
    return (top == -1);
    }
    //--------------------------------------------------------------
    } // end class StackX

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    often, the compiler tells you all you need to know. the variable "normal" might not have been initialized.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default value == initalized

    sfe23... various persons have told you to try to understand what the error is trying to tell you. To initialize a variable means to give it a value.
    Java Code:
    String normal,rev, output;
    int dir;
    Here "normal" is defined as String and "dir" is defined as an int... but they have NO value assigned to them: they haven't been initialized.
    Assign them a value ...
    Java Code:
    String normal = monkey;
    int dir = 1;
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    I understand what the compiler tells me but doesn't this
    Reverser theReverser = new Reverser(normal,dir);
    call the constructor which initializes normal depending on what dir is??

  11. #11
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Nevermind, I fixed the initialization problem. thanks fo ryour help

Similar Threads

  1. Method and Constructor
    By swikar.java in forum New To Java
    Replies: 7
    Last Post: 10-21-2008, 03:41 PM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 09:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 06:44 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
  •