Home > Mobile >  How do I add first name and last name in vector and display all names in vector?
How do I add first name and last name in vector and display all names in vector?

Time:10-17

I am new to java and learning through a task.

I am creating a program which will display following options to a user:

1.accept first name and surname

2.display total name

3.Exit

When a user click on option 1, It should ask user "first name & last name" and add same values to Vector Object

While the option 2 will display all the stored names from vector object.

I have tried to create a program but when I input my name it is raising exception: InputMismatchException

Please have a look at my code.

Name.java

package LearnJava;

public class Name {
    String firstName,surName;

    public Name(String fName, String sName) {
        super();
        this.firstName = fName;
        this.surName = sName;
    }

    public String getName() {
        return firstName;
    }

    public void setName(String fName) {
        this.firstName = fName;
    }

    public String getSname() {
        return surName;
    }

    public void setSname(String sName) {
        this.surName = sName;
    }


}

Main.java Main class file

package LearnJava;

import java.util.Scanner;
import java.util.Vector;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the choice:");
        System.out.println("\n1.Accept first name and surname\n2.display total name\n3.Exit.");

        int choice=sc.nextInt();
        Vector v1=new Vector ();
        Vector<Name> v2=new Vector<Name>();

        while(choice !=3)
        {
            if(choice==1)
            {
                System.out.println("enter the first name and surname:");
                int n=sc.nextInt();
                for(int i=0;i<=n;i  )
                {
                    v1.add( new Name( sc.next(), sc.next()));
                }
            }
            System.out.println("enter the choice:");
            System.out.println("\n1.Accept first name and surname\n \n2.display total name\n3.Exit.");
                    choice=sc.nextInt();
            if(choice==2)
            {
                System.out.println("Here is total:" v1.size());
            }
        }
        System.out.println("thank you");
    }


}


CodePudding user response:

If I see it correctly and you are just entering your name the problem is the scanning for int to get n. n is an int and the scanner will get your name which is a string which will result in the mismatch exception. Also v1 is unnecessary vector of objects as you know you are only giving it names, you should write the type name so you could later use the properties of Name.

CodePudding user response:

because your actually use nextInt() method which parse the input to integer which means the input should integer. your code just need a little reformat have a look to class Main :)

package LearnJava;
import java.util.Scanner;
import java.util.Vector;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the choice:");
        System.out.println("\n1.Accept first name and surname\n2.display total name\n3.Exit.");

    int choice=sc.nextInt();
    Vector v1=new Vector ();
    Vector<Name> v2=new Vector<Name>();

    while(choice !=3)
    {
        if(choice==1)
        {
            System.out.println("enter the number of people:");

            int n=sc.nextInt();
            for(int i=0;i<=n;i  )
            {
                System.out.println("enter the first name and surname:");
                v1.add( new Name( sc.next(), sc.next()));
            }
        }
        System.out.println("enter the choice:");
        System.out.println("\n1.Accept first name and surname\n \n2.display total name\n3.Exit.");
                choice=sc.nextInt();
        if(choice==2)
        {
            System.out.println("Here is total:" v1.size());
        }
    }
    System.out.println("thank you");
}

}

CodePudding user response:

Your problem is not with vector , it is with scanner methods , you can see this answer it will give you a good idea about how to use scanner methods : Scanner is skipping nextLine() after using next() or nextFoo()?

"That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline. You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself)."

your code working:

 public class Main {
 public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the choice:");
    System.out.println("\n1.Accept first name and surname\n2.display total name\n3.Exit.");
    int choice=Integer.parseInt(sc.nextLine());
    Vector v1=new Vector ();
    Vector<Name> v2=new Vector<Name>();

    while(choice !=3)
    {
        if(choice==1)
        {
            System.out.println("enter number of persons :");
            int n=Integer.parseInt(sc.nextLine());
            for(int i=1;i<=n;i  )
            {
                System.out.println("enter the first name of person number :" (i));
                String fn =sc.nextLine();
                System.out.println("enter the last name of person number :" (i));
                String ln =sc.nextLine();
                 v1.add( new Name( fn, ln));
            }
        }
        System.out.println("\n\n Enter the choice:");
        System.out.println("\n1.Accept first name and surname\n \n2.display total name\n3.Exit.");
                choice=Integer.parseInt(sc.nextLine());
        if(choice==2)
        {
            System.out.println("\n ********************** Here is total:" v1.size() "\n\n");
        }
    }
    System.out.println("thank you");
}
}
  • Related