-4

We had a programming exam last week and I wasn't able to do it.

We were asked to make a program that will ask the user to select either of these three options:

  • given name
  • middle name
  • last name.

After selecting, you are asked to enter a name using scanner.

Then it will ask you if you want to try again, if you input yes, the program will loop again and if you enter the same name, it will prompt an error saying you already set it that way.

I've done all of it except that part, comparing the values.

How do I compare the 1st and 2nd input using scanner?

Here is what I have done so far.


public class PXM {
    private String givenname;
    private String middlename;
    private String lastname;

    public void setGN(String gn) {
        this.givenname = gn;
    }
    public void setMN(String mn) {
        this.middlename = mn;
    }
    public void setLN(String ln) {
        this.lastname = ln;
    }
    public boolean equals() {
    }
}

import java.util.*;

public class TestPXM {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String yes;
        int t;

        PXM p1 = new PXM(); {
            System.out.println("1. given name ");
            System.out.println("2. middle name");
            System.out.println("3. last name");

            System.out.print("select method: ");
            int method = sc.nextInt();
            if (method == 1) {
                System.out.println("enter given name: ");
                String gn = sc.next();
                p1.setGN(gn);
            }
        }
    }
}
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
Gostores
  • 9
  • 1
  • 2

3 Answers3

0

You don't use the scanner to compare. You would save the values input and compare against those.

I would use a Set to hold the values, because Set values are guaranteed to be unique, and that suits the application. You would use Set.contains() to find if you've already got a name.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Or use if(Set.add()) so if it is unique it is added already in one line, and if not, you can use recursion in the else, or handle it there – CrackerJack9 Aug 07 '11 at 21:04
  • i didnt say ill use scanner to compare. i said,i want to compare the values inputted using the scanner . – Gostores Aug 08 '11 at 12:35
0

Assuming you have two Strings, s1 and s2 and you want to compare them, you should use the equals() method:

if (s1.equals(s2)) {
    // strings are the same
} else {
    // strings are different
}

If you don't know how to use Scanner, take a look here.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

You can use this: Since you've already set the variable in:

p1.setGN(gn);

you can read the input again after suitable prompt like:

String gn=sc.next();

and then compare the stored variable in the object p1 using a getter method:

public String getGN()
{
return this.givenname;
}

Now if you do

if(gn.equals(p1.getGN())
{
System.out. println("You have already set that");
}

You will get the desired result.

Sharath
  • 65
  • 5