简体   繁体   中英

Getting a String from one part of the class to the other

ORIGINAL QUESTION

so I am new to coding. Ive probably hit my 3 month mark. But I like to go past the class I am taking because this stuff really interests me. So I wanted to mess around with some code to try and understand it some more. After a lot of googling this is as far as I have gotten. The program is suppose to ask for a password. If the right password is entered then it will show two options. Option 1 will have you put information in (name, last name, age, cell phone number). Option 2 will show the information stored. Everything so far has been going great besides the fact I want to display the information gained from A into B. I have a two separate classes. The first is called main (This is the main method witch works fine)

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        while(!input.equals(passWord)) //This loop looks for the password
        {

        input = JOptionPane.showInputDialog("Hello, please enter password.");

        if(input.equals(passWord)) //If the password is correct
        {
            while(!input.equals("Enter information")||!input.equals("View profile")) //This loop looks to see what to do next
            {
                input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile");

                if(input.equals("Enter information"))
                {
                    display.input();
                }
                else if(input.equals("View profile"))
                {
                    display.stored();
                }
                else
                {
                tempString = "ERROR\nCannot find what you are looking for.";
                JOptionPane.showMessageDialog(null, tempString);
                }
            }


        }
        else //If the password is incorrect.
        {
            tempString = "In-correct";
            JOptionPane.showMessageDialog(null, tempString);
        }
    }
    }
}

My second class (display) is where I have been running into problems. Should I make them Public Strings? Or what? The input() method fills the Strings that I want to use in the stored() method. And I have been looking this up for awhile but I don't understand the return and what not. If you could help me out and point out my flaws that would be fantastic.

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class display
{
    public static void input() //This is the method that will ask for the information
    {
        String age="null", cellNumber="null", name="null", lastName="mull", allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";

        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");

        display.stored();

    }


    public static void stored() //This method is asking the user what to show for the input() method.
    {
        String loop = "loop", tempString;


    while(!loop.equals("break"))
    {

    tempString = JOptionPane.showInputDialog("What information would you like to see? \nname\nage\ncell number\nall info\nquit");

    if(tempString.equals("name")||tempString.equals("Name")||tempString.equals("NAME"))
    {
        JOptionPane.showMessageDialog(null, name); //This is where I want to display the name String from input() method
    }
    else if(tempString.equals("age")||tempString.equals("Age")||tempString.equals("AGE"))
    {
        JOptionPane.showMessageDialog(null, age); //This is where I want to display the age String from input() method
    }
    else if(tempString.equals("cell number")||tempString.equals("Cell number")||tempString.equals("cell Number")||tempString.equals("Cell Number")||tempString.equals("cellNumber")||tempString.equals("cellnumber")||tempString.equals("Cellnumber"))
    {
        JOptionPane.showMessageDialog(null, cellNumber); //This is where I want to display the cellNumber String from input() method
    }
    else if(tempString.equals("all info")||tempString.equals("All info")||tempString.equals("all Info")||tempString.equals("All Info")||tempString.equals("allinfo")||tempString.equals("allInfo")||tempString.equals("Allinfo")||tempString.equals("AllInfo"))
    {
        JOptionPane.showMessageDialog(null, allInfo); //This is where I want to display the allInfo String from input() method
    }
    else if(tempString.equals("quit")||tempString.equals("Quit")||tempString.equals("QUIT"))
    {
        loop = "break"; //Breaks the while loop
    }
    else
    {
        tempString = "Not a valid answer. \nPlease try again.";
        JOptionPane.showMessageDialog(null, tempString);
    }
}
}
}

UPDATED QUESTION

Okay so after looking at the answers I got it really close! But for some reason when I go to look at the data it produces "null" for everything. I'm thinking its because II close the method and then re open it so everything refreshes. How do I save the information put in input. Leave the method. Come back but open display instead and show that information?

Here is the updated code:

main class

import javax.swing.*;

//Created by Robert Duval
//3/26/13

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        display display = new display();

        while(!input.equals(passWord)) //This loop looks for the password
        {

            input = JOptionPane.showInputDialog("Hello, please enter password.");

            if(input.equals(passWord)) //If the password is correct
            {
                while(!input.equalsIgnoreCase("Enter information")||!input.equalsIgnoreCase("View profile")) //This loop looks to see what to do next
                {
                    input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile");

                    if(input.equalsIgnoreCase("Enter information"))
                    {
                        display.input();
                    }
                    else if(input.equalsIgnoreCase("View profile"))
                    {
                        display.stored();
                    }
                    else
                    {
                        tempString = "ERROR\nCannot find what you are looking for.";
                        JOptionPane.showMessageDialog(null, tempString);
                    }
                }
            }
            else //If the password is incorrect.
            {
                tempString = "In-correct";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

display class

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class display
{
    String age="null", cellNumber="null", name="null", lastName="mull", allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");
    }


    public void stored()
    {
        String tempString;


        while(true)
        {

            tempString = JOptionPane.showInputDialog("What information would you like to see? \nname\nage\ncell number\nall info\nquit");

            if (tempString.equalsIgnoreCase("name"))
            {
                JOptionPane.showMessageDialog(null, name); //This is where I want to display the name String from input() method
            }
            else if(tempString.equalsIgnoreCase("age"))
            {
                JOptionPane.showMessageDialog(null, age); //This is where I want to display the age String from input() method
            }
            else if(tempString.equalsIgnoreCase("cell number"))
            {
                JOptionPane.showMessageDialog(null, cellNumber); //This is where I want to display the cellNumber String from input() method
            }
            else if(tempString.equalsIgnoreCase("all info")||tempString.equalsIgnoreCase("allinfo"))
            {
                JOptionPane.showMessageDialog(null, allInfo); //This is where I want to display the allInfo String from input() method
            }
            else if(tempString.equalsIgnoreCase("quit"))
            {
               break; //Breaks the while loop
            }
            else
            {
                tempString = "Not a valid answer. \nPlease try again.";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

BTW thanks everyone for all the help. I appreciate it.

SOLUTION

Alright guys. I played with it some more and found out how to get it to work. Thanks for all of the help it was needed.

main class

import javax.swing.*;

//Created by Robert Duval
//3/26/13

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        display display = new display();

        while(!input.equals(passWord)) //This loop looks for the password
        {

            input = JOptionPane.showInputDialog("Hello, please enter password.\nQuit");

            if(input.equals(passWord)) //If the password is correct
            {
                while(!input.equalsIgnoreCase("Enter information")||!input.equalsIgnoreCase("View profile")) //This loop looks to see what to do next
                {
                    input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile\nLog out");

                    if(input.equalsIgnoreCase("Enter information"))
                    {
                        display.input();
                    }
                    else if(input.equalsIgnoreCase("View profile"))
                    {
                        display.stored();
                    }
                    else if(input.equalsIgnoreCase("log out"))
                    {
                        break;
                    }
                    else
                    {
                        tempString = "ERROR\nCannot find what you are looking for.";
                        JOptionPane.showMessageDialog(null, tempString);
                    }
                }
            }
            else if(input.equalsIgnoreCase("quit"))
            {
                break;
            }
            else //If the password is incorrect.
            {
                tempString = "In-correct";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

display class

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class display
{
    String age="null", cellNumber="null", name="null", lastName="null";

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");
    }


    public void stored()
    {
        String tempString, allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";


        while(true)
        {

            tempString = JOptionPane.showInputDialog("What information would you like to see? \nName\nAge\nCell number\nAll info\nBack");

            if (tempString.equalsIgnoreCase("name"))
            {
                JOptionPane.showMessageDialog(null, name);
            }
            else if(tempString.equalsIgnoreCase("age"))
            {
                JOptionPane.showMessageDialog(null, age);
            }
            else if(tempString.equalsIgnoreCase("cell number"))
            {
                JOptionPane.showMessageDialog(null, cellNumber);
            }
            else if(tempString.equalsIgnoreCase("all info")||tempString.equalsIgnoreCase("allinfo"))
            {
                JOptionPane.showMessageDialog(null, allInfo);
            }
            else if(tempString.equalsIgnoreCase("back"))
            {
               break;
            }
            else
            {
                tempString = "Not a valid answer. \nPlease try again.";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

It runs perfectly!

PS It wouldn't let me answer my own question

You need to make your display class methods non-static and use an object with fields:

public class Display
{
    private String age="null", cellNumber="null", name="null", lastName="mull", allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");

        stored();
    }        

    public void stored()
    {
        String tempString;

        while(true)
        {        
            tempString = JOptionPane.showInputDialog("What information would you like to see? \nname\nage\ncell number\nall info\nquit");

            if(tempString.equalsIgnoreCase("name"))
            {
                JOptionPane.showMessageDialog(null, name); //This is where I want to display the name String from input() method
            }
            else if(tempString.equalsIgnoreCase("age"))
            {
                JOptionPane.showMessageDialog(null, age); //This is where I want to display the age String from input() method
            }
            else if(tempString.equalsIgnoreCase("cell number")||tempString.equals("Cell number")||tempString.equalsIgnoreCase("cellNumber"))
            {
                JOptionPane.showMessageDialog(null, cellNumber); //This is where I want to display the cellNumber String from input() method
            }
            else if(tempString.equalsIgnoreCase("all info")||tempString.equalsIgnoreCase("allinfo"))
            {
                JOptionPane.showMessageDialog(null, allInfo); //This is where I want to display the allInfo String from input() method
            }
            else if(tempString.equalsIgnoreCase("quit"))
            {
               break; //Breaks the while loop
            }
            else
            {
                tempString = "Not a valid answer. \nPlease try again.";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

As you cann see I changed the class name from display to Display . This is a Java convention to distuingish class names from variable names. The methods are not static anymore, meaning you can't call them on the class, but only on an object of that class. Such an object can have fields describing it's condtion. To have access to those fields you need non-static members. The call display.stored() is now just stored() to call the method on the same object you just called input() on. To clearify it, you could also write this.stored() . this always point to the present object.

I also introduced the break command to the loop in the Display class.

Let's take a look what changes have to be made in your main class now:

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        Display display = new Display();

        while(!input.equals(passWord)) //This loop looks for the password
        {
            input = JOptionPane.showInputDialog("Hello, please enter password.");

            if(input.equals(passWord)) //If the password is correct
            {
                while(!input.equals("Enter information")||!input.equals("View profile")) //This loop looks to see what to do next
                {
                    input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile");

                    if(input.equals("Enter information"))
                    {
                        display.input();
                    }
                    else if(input.equals("View profile"))
                    {
                        display.stored();
                    }
                    else
                    {
                        tempString = "ERROR\nCannot find what you are looking for.";
                        JOptionPane.showMessageDialog(null, tempString);
                    }
                }
            }
            else //If the password is incorrect.
            {
                tempString = "In-correct";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

The line Display display = new Display() creates a new object of the Display class and assigns it to a variable with type Display and name display . If you call methods on display (which is now the variable) they are invoked on the object the variable points to instead of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM