I tried to create a simple To-Do List program. It asks how many items you want in your list, it asks for each item, and it asks you if you want to display the items. However, I'm having a runtime error. When the program asks for the first item, it doesn't allow me to enter what my first list item is. When it lists my items in numerical order, the first item on the list (which I couldn't input text for, it skipped past it after displaying the text) appears twice. So, if I had 3 items on the list, here's what it would display:
1.
1.
2. (Second item)
3. (Third item)
I've combed through the code, redone parts, and I can't find the issue. Here is my code:
import java.util.Scanner;
/*
This is a program that will
help you create a To-Do List
*/
public class ToDoList
{
public static void main(String[] args)
{
int numOfItems = -1, answer;
Scanner keyboard = new Scanner(System.in);
System.out.println("\n\nHello!\nThis is a program that will " +
"help you create a To-Do List.");
while (numOfItems < 0)
{
System.out.println("\nHow many items do you want to add?");
if (keyboard.hasNextInt())
numOfItems = keyboard.nextInt();
}
String[] itemsArray = new String[numOfItems];
for (int increasingNum = 0; numOfItems > increasingNum; increasingNum++)
{
System.out.println("\nPlease enter item number: " + (increasingNum + 1));
itemsArray[increasingNum] = keyboard.nextLine();
}
System.out.println("\nCongratulations! You have successfully created a To-Do List." +
"\nWould you like to view the list now?" +
"\n\nEnter:\n\t\"1\" for Yes\n\t\"2\" for No");
answer = keyboard.nextInt();
if (answer == 1)
{
for (int secondIncreasingNum = 0; numOfItems > secondIncreasingNum; secondIncreasingNum++)
{
System.out.println((secondIncreasingNum + 1) + ". " + itemsArray[secondIncreasingNum]);
}
}
System.exit(0);
}
}
EDIT: I've fixed the first problem when it asks you to enter item number one, but the error at the end still occurs. It now lists it like this:
- (empty) 1.(item 1) 2.(item 2) etc.