75 lines
1.2 KiB
Java
75 lines
1.2 KiB
Java
|
import java.util.ArrayList;
|
||
|
import java.util.Scanner;
|
||
|
/*
|
||
|
* Take in single-word first names and ages
|
||
|
* end with -1
|
||
|
* program outputs list with age incremented
|
||
|
* program fails and throws exception if second input on a line is a string
|
||
|
*
|
||
|
*
|
||
|
* EX:
|
||
|
* Input:
|
||
|
* Lee 18
|
||
|
* Lua 21
|
||
|
* Mary Beth 19
|
||
|
* Stu 33
|
||
|
* -1
|
||
|
*
|
||
|
* Output:
|
||
|
* Lee 19
|
||
|
* Lua 22
|
||
|
* Mary 0
|
||
|
* Stu 34
|
||
|
*/
|
||
|
|
||
|
public class main {
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
Scanner scnr = new Scanner(System.in);
|
||
|
String s = "";
|
||
|
ArrayList<String> userInputs = new ArrayList<String>();
|
||
|
String name;
|
||
|
int age;
|
||
|
|
||
|
//Get input
|
||
|
while(s != "-1"){
|
||
|
System.out.println("Enter a name and age. -1 to exit: ");
|
||
|
|
||
|
//Get name or exit
|
||
|
s = scnr.next();
|
||
|
|
||
|
if (s.equals("-1")){
|
||
|
System.out.println();
|
||
|
break;
|
||
|
}
|
||
|
else{
|
||
|
name = s;
|
||
|
}
|
||
|
|
||
|
//Get age
|
||
|
|
||
|
try{
|
||
|
age = scnr.nextInt() +1;
|
||
|
}
|
||
|
catch(Exception e){
|
||
|
System.out.println("Age must be an Integer\n" +
|
||
|
"Setting age to 0 ");
|
||
|
age = 0;
|
||
|
|
||
|
}
|
||
|
//Finish line
|
||
|
scnr.nextLine();
|
||
|
|
||
|
//Add name plus new age to arr
|
||
|
userInputs.add(name + " " + Integer.toString(age));
|
||
|
|
||
|
System.out.println();
|
||
|
}
|
||
|
|
||
|
for (String S:userInputs){
|
||
|
System.out.println(S);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|