Jump to content

Printing a line within the same class in a separate method in Java using scanners

Lyons

Help thread....

 

Is there a way I can write an if statement that prints a massage that I've entered into a scanner?

 

ie

 

Story()

{

 

}

 

boolean newStory

{

Scanner story = new Scanner(System.in);

System.out.println("Add a story");
String newStory3 = story.next();
}
 
boolean shareStory
{
Scanner shareStory = new Scanner(System.in);

System.out.println("Enter 'share' to share your last story");

String shareStory = shareStory.next();

if (shareStory == "share")
        {
            System.out.println(WHAT DO I PUT HERE?);
            return true;
        }
        else
        {
            return false;
        }

}

 

Not sure if I'm on the right track or not... correct me if I'm wildly wrong  :)

 

Laptop Lenovo Thinkpad X220 - CPU: i5 2420m - RAM: 8gb - SSD: Samsung 830 - IPS screen Peripherals Monitor: Dell U2713HM - KB: Ducky shine w/PBT (MX Blue) - Mouse: Corsair M60

Audio Beyerdynamic DT990pro headphones - Audioengine D1 DAC/AMP - Swan D1080-IV speakers

Link to comment
Share on other sites

Link to post
Share on other sites

Oh jesus, I double posted.

Laptop Lenovo Thinkpad X220 - CPU: i5 2420m - RAM: 8gb - SSD: Samsung 830 - IPS screen Peripherals Monitor: Dell U2713HM - KB: Ducky shine w/PBT (MX Blue) - Mouse: Corsair M60

Audio Beyerdynamic DT990pro headphones - Audioengine D1 DAC/AMP - Swan D1080-IV speakers

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, store it as a variable that the main class can access and print it out.

Link to comment
Share on other sites

Link to post
Share on other sites

Yup, you're on the right track.

Remember, though, that with java the "==" operator will not work with strings, only primitives (int,long,char,etc). WIth strings, the "==" operator actually compares object references. To compare two strings, use string1.equals(string2).

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×