My static method to add an Object to an Arraylist of Objects.
public static void addObject() {
int id;
String name;
try{
System.out.print("Id: ");
id = Integer.parseInt(sc.next());
System.out.print("Name: "); //when the program gets here, he just skips back to my main class...
name = sc.nextLine();
Object o = new Object(id, name);
l.addObject(o); //adds the object to this list (l) of objects
}
catch(NumberFormatException nfe){
System.out.println("Not a valid id!");
addObject();
}
}
My main method which contains a switch in do-while-loop, which adds, deletes and edits objects.
public static void main(String[] args){
int choice;
do{
try{
choice = Integer.parseInt(sc.next());
switch (choice){
case 0: break; //ends the program
case 1: addObject(); break; //starting static method to add an object with a name and an id
//here are some more cases with similar static methods (left them out for simplicity)
default: System.out.println("Not a valid choice!");break;
}
}
catch(NumberFormatException nfe){
System.out.println("Not a valid choice!");
choice = -1; //to keep the loop running, and prevent another exception
}
}while (choice != 0);
System.out.println("Good bye!");
}
My Object class
public class Object{
private int id;
private String name;
public Object(int id, String name) {
this.id = id;
this.name = name;
}
}
My ObjectList class
import java.util.*;
public class ObjectList {
private List<Object> objects;
public ObjectList() {
objects = new ArrayList<Object>();
}
public void addObject(Object o){
objects.add(d);
}
}
When I try to run the static method to add an Object, it records the id of the object just fine, but when I enter the objects id, it goes back to my main method, starting the loop all over. It reacts just fine when I enter a string in the switch(restarting the loop). But I can't seem to add objects properly.
This is also a school assignment, in which they gave us all this code (except for the try-catch methods), and asked us to write a try-catch for the static method and the main method. I could probably find a workaround for the main method with an if-clause, but I was wondering if this is possible with a try-catch method.
Issues:
nextLine()
for instance) and those that don't ( nextInt()
for instance). Note that the former, nextLine()
swallows the end of line (EOL) token while nextInt()
and similar methods don't. So if you call nextInt()
and leave an end of line token tangling, calling nextLine()
will not get your next line, but will rather swallow the dangling EOL token. One solution is to call sc.nextLine()
right after calling sc.nextInt()
just to hande the EOL token. Or else where you call sc.next()
, change that to sc.nextLine()
. For example, if you had this code:
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print("Enter name: ");
String name = sc.nextLine();
You'll find that the name is always ""
, and that is because the sc.nextLine()
is swallowing the end of line (EOL) token left over from the user's entering a number. One way to solve this is to do:
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
sc.nextLine(); // ***** to swallow the dangling EOL token
System.out.print("Enter name: ");
String name = sc.nextLine();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.