Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help! Error Message (I want to be able to add an int for number, not a string) >

ID: 3831148 • Letter: H

Question

Help! Error Message (I want to be able to add an int for number, not a string)

>>Exception in thread "main" java.util.InputMismatchException
§ at java.util.Scanner.throwFor(Scanner.java:864)
§ at java.util.Scanner.next(Scanner.java:1485)
§ at java.util.Scanner.nextInt(Scanner.java:2117)
§ at java.util.Scanner.nextInt(Scanner.java:2076)
§ at MyContacts.driver(MyContacts.java:26)
§ at MyContacts.<init>(MyContacts.java:16)

import java.util.*;

------------------------------------------------------

public class MyContacts
{
   Scanner sc;
   int number;
   String name;
   String address;
  
   public ArrayList<Contact> con;
   public MyContacts()
   {
   sc = new Scanner(System.in);
   con = new ArrayList<Contact>();
   driver();
   }

   public void driver(){
   int option,i,j;

   while(true)
   {
   menu();
   System.out.printf(">>");
   option = sc.nextInt();
  
   if(option == 1)
   {
  
   Contact c=creatContact();
   con.add(c);
   System.out.println("Your contact has been added");
   }

   else if(option == 2)
   {
  
   System.out.println();
   i = sc.nextInt();
   con.remove(i);
   System.out.println("Your contact has been removed");
   }
   else if(option == 3)
   {
   display();
   System.out.println("Enter index to change order:");

   i = sc.nextInt();
   j = sc.nextInt();

   if((i >= 0 && i<con.size()) && (j >= 0 && j<con.size())){
   swap(i,j);
   System.out.println("You contacts have been rearranged.");
   display();

   }
   else{
   System.out.println("You have chosen an invalid number. Please submit a valid entry to try again.");
   }

   }
   else if(option == 4)
   {
  
   display();
   }
   else if(option == 5)
   {
   break;
   }

   }
   }
   public void display()
   {
   System.out.println("Your contacts are:");
  
   for(int i=0; i<con.size(); i++)
{
System.out.println(i+" - "+con.get(i));
}
  
   System.out.println();
   }
   public void menu()
   {
   System.out.println("Please select one of the following options to update your contact list.");
   System.out.println("1.Add");
   System.out.println("2.Remove");
   System.out.println("3.Change Order");
   System.out.println("4.Display");
   System.out.println("5.Exit");
   }

   public Contact creatContact()
   {

   System.out.printf("Name:");
   name = sc.next();

   System.out.printf("Number:");
   number = sc.nextInt();

   System.out.printf("Address:");
   address = sc.next();

   Contact c = new Contact(this.name,this.number,this.address);
   return c;
   }
  
   public void swap(int i,int j)
   {
   Contact c1=con.get(i);
   Contact c2=con.get(j);

   Contact temp = new Contact();
   temp.name=c2.name;
   temp.number=c2.number;
   temp.address=c2.address;

   c2.name=c1.name;
   c2.number=c1.number;
   c2.address=c1.address;

   c1.name=temp.name;
   c1.number=temp.number;
   c1.address=temp.address;
   }
   public static void main(String args[])
   {
   new MyContacts();

   }
}

class Contact
{
  
   String name;
   int number;
   String address;
  
   public Contact()
   {
   }
  
   public Contact(String name,int number,String address)
   {
   this.name=name;
   this.number=number;
   this.address=address;
   }

   public String toString()
   {
   return (this.name+" "+this.number+" "+this.address);
   }
}

Explanation / Answer

Hi, the issue with your program was that you were trying to use int for number. But number is bigger than the range of int. So we need to use long in place of that. I have changed your code and will highlight(BOLD TEXT) the change in your code: -

MyContacts.java

import java.util.ArrayList;

import java.util.Scanner;

public class MyContacts {

       Scanner sc;

       long number;

       String name;

       String address;

       public ArrayList<Contact> con;

       public MyContacts() {

              sc = new Scanner(System.in);

              con = new ArrayList<Contact>();

              driver();

       }

       public void driver() {

              int option, i, j;

              while (true) {

                     menu();

                     System.out.printf(">>");

                     option = sc.nextInt();

                     if (option == 1) {

                           Contact c = creatContact();

                           con.add(c);

                           System.out.println("Your contact has been added");

                     }

                     else if (option == 2) {

                           System.out.println();

                           i = sc.nextInt();

                           con.remove(i);

                           System.out.println("Your contact has been removed");

                     } else if (option == 3) {

                           display();

                           System.out.println("Enter index to change order:");

                           i = sc.nextInt();

                           j = sc.nextInt();

                           if ((i >= 0 && i < con.size()) && (j >= 0 && j < con.size())) {

                                  swap(i, j);

                                  System.out.println("You contacts have been rearranged.");

                                  display();

                           } else {

                                  System.out.println("You have chosen an invalid number. Please submit a valid entry to try again.");

                           }

                     } else if (option == 4) {

                           display();

                     } else if (option == 5) {

                           break;

                     }

              }

       }

       public void display() {

              System.out.println("Your contacts are:");

              for (int i = 0; i < con.size(); i++) {

                     System.out.println(i + " - " + con.get(i));

              }

              System.out.println();

       }

       public void menu() {

              System.out.println("Please select one of the following options to update your contact list.");

              System.out.println("1.Add");

              System.out.println("2.Remove");

              System.out.println("3.Change Order");

              System.out.println("4.Display");

              System.out.println("5.Exit");

       }

       public Contact creatContact() {

              System.out.printf("Name:");

              name = sc.next();

              System.out.printf("Number:");

              number = sc.nextLong();

              System.out.printf("Address:");

              address = sc.next();

              Contact c = new Contact(this.name, this.number, this.address);

              return c;

       }

       public void swap(int i, int j) {

              Contact c1 = con.get(i);

              Contact c2 = con.get(j);

              Contact temp = new Contact();

              temp.name = c2.name;

              temp.number = c2.number;

              temp.address = c2.address;

              c2.name = c1.name;

              c2.number = c1.number;

              c2.address = c1.address;

              c1.name = temp.name;

              c1.number = temp.number;

              c1.address = temp.address;

       }

       public static void main(String args[]) {

              new MyContacts();

       }

}

Contact.java

class Contact {

       String name;

       long number;

       String address;

       public Contact() {

       }

       public Contact(String name, long number, String address) {

              this.name = name;

              this.number = number;

              this.address = address;

       }

       public String toString() {

              return (this.name + " " + this.number + " " + this.address);

       }

}

Sample Output: -

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>1

Name:Sal

Number:997717688298

Address:hfchg

Your contact has been added

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>1

Name:vidhh

Number:989898767665

Address:hcghj

Your contact has been added

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>4

Your contacts are:

0 - Sal 997717688298 hfchg

1 - vidhh 989898767665 hcghj

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>3

Your contacts are:

0 - Sal 997717688298 hfchg

1 - vidhh 989898767665 hcghj

Enter index to change order:

0

1

You contacts have been rearranged.

Your contacts are:

0 - vidhh 989898767665 hcghj

1 - Sal 997717688298 hfchg

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>2

1

Your contact has been removed

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>4

Your contacts are:

0 - vidhh 989898767665 hcghj

Please select one of the following options to update your contact list.

1.Add

2.Remove

3.Change Order

4.Display

5.Exit

>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote