public void compareStrings()
This method will prompt the user to type two strings and will put them into different
String variables. Then it will compare those strings to see if they are the same, i.e. have
the same letters in the same order. If the strings are the same, “same” will be displayed
on the screen. If they are different, “different” will be displayed. To compare two String
objects you must use the equals() method of the String class.

How do I do this?

Recommended Answers

All 15 Replies

DaniWeb Member Rules include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"

What have you done so far?

You got algorithm given, now just implement it.

This is what I have so far:
System.out.print("Type a sentence: ");
String userInput1 = reader.getInput();
System.out.print("Type another sentence: ");
String userInput2 = reader.getInput();
I don't know how to write the if statement. Can you help me?

Thanks but I need more help to write the if statement...

Which part of the if statement are you having problems with?
The comparing of the two Strings?
For the rest, copy one of the if statements from the tutorial.

Inline Code Example Here

if(userInput1 == userInput2) {
   System.out.print("Same");}
else {
   System.out.print("Not same"); }
I am still getting an error message, what am I doing wrong?
public void compareStrings()
This method will prompt the user to type two strings and will put them into different
String variables. Then it will compare those strings to see if they are the same, i.e. have
the same letters in the same order. If the strings are the same, “same” will be displayed
on the screen. If they are different, “different” will be displayed. To compare two String
objects you must use the equals() method of the String class.



 This is what I have:
    public void compareStrings()
    {
        System.out.print("Type a sentence: ");
        String userInput1 = reader.getInput();
        System.out.print("Type another sentence: ");
        String userInput2 = reader.getInput();
        if(userInput1.equals (userInput2))  {
           System.out.println("Same");  }
        else  {


       System.out.println("Not Same");  
    System.out.println();



 Does this look right?

Does it compile and execute? That is the best way to tell if something is right.
If you get error messages, copy and paste the full text here.

You will need to put it in a class and call it from the main() method.

yes I have it in a class.. Do you need to see the whole object? I can upload it. I am taking an introductory class for Java and we are using the BlueJ environment.

This lab involves working with methods from the String class and getting user input from
the keyboard. To get user input, put this class in your project.
import java.util.Scanner;
/**
* Class InputReader reads user input from the keyboard.
* @version 2009.10.25
* @author Colleen Penrowley
*/
public class InputReader
{
private Scanner scanner;
/**
* Create a new InputReader to read user input.
*/
public InputReader()
{
scanner = new Scanner(System.in); // for keyboard input
}
/**
* @return the user's input as a String
*/
public String getInput()
{
return scanner.nextLine();
}
}
Create a second class called StringPlay that has a field of type InputReader. In this
class you will write a method that does these things:
 asks the user to type something (and reads it in)
 displays what the user typed
 reports how many letters are in what the user typed
 displays what the user typed in all upper case
 displays what the user typed in all lower case
This method must call methods of the String class.
Here's how to get started.
public class StringPlay
{
private InputReader reader;
public StringPlay()
{
reader = new InputReader();
}
Now write a method that uses reader to get keyboard input by calling the getInput()
method. Here's how to get started.
public void playWithStrings()
{
System.out.print(“Type a sentence: ”);
String userInput = reader.getInput();
// you do the rest
Here's an example of what might be on the screen. The part in blue is the prompt to the
user. The part in red is what the user has typed.
Type a sentence: What sort of sentence?
You typed: What sort of sentence?
Your sentence has 22 letters.
Here it is uppercase: WHAT SORT OF SENTENCE?
Here it is lowercase: what sort of sentence?
Write a second method with this signature:
public void compareStrings()
This method will prompt the user to type two strings and will put them into different
String variables. Then it will compare those strings to see if they are the same, i.e. have
the same letters in the same order. If the strings are the same, “same” will be displayed
on the screen. If they are different, “different” will be displayed. To compare two String
objects you must use the equals() method of the String class.

Don't need to see anything if the code compiles and executes. If you are getting errors, then you need to post the full text of the error message and the code that is causing the error.

This is what I have done so far.

Hey Thanks NormR1 I figured it out....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.