0

I am new to Java and learning through a task.

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

Here is 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;
    }


}

halfer
  • 19,824
  • 17
  • 99
  • 186
Neha Sharma
  • 461
  • 2
  • 7
  • 21
  • 2
    Your code logic is a bit convoluted and messed up, but actually, the best solution is for you *you* to do some debugging to see where it is going wrong. If you're not sure how to go about doing this, then please check out [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Also, always ask yourself when creating a line of code if it makes sense? You must learn to do mental walk-throughs of your code, asking yourself the very same question as you do it -- also known as "[Rubber Duck Debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging)". – Hovercraft Full Of Eels Oct 16 '21 at 11:40
  • 1
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Oussama ZAGHDOUD Oct 16 '21 at 12:13

3 Answers3

1

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.

SgtOmer
  • 200
  • 1
  • 1
  • 8
1

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");
}

}

Mazen Embaby
  • 1,255
  • 11
  • 18
1

Your problem is not with vector, it is with scanner methods; you can read this answer, it will give you a good idea about how to use scanner methods.

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");
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Oussama ZAGHDOUD
  • 1,767
  • 5
  • 15