0

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:

  1. (empty) 1.(item 1) 2.(item 2) etc.
noahj98
  • 11
  • 1
  • I think you are confusing the 1 that was input as the option to print the list. Add `System.out.println("-----")` before you print the list to see if this is the case. – Java Devil Jul 06 '17 at 04:33

2 Answers2

0

The problem is that you have a newline remaining in the scanner from after you have called nextInt() for getting the number of items you want to enter.

So the first call to nextLine() get that line which is empty.

Just add keyboard.nextLine() after your while loop to get the number of items, but before your for loop for the item text to clear it.


Also you have a lot of unnecessary variables declared for example in your loops:

for (int increasingNum = 0; numOfItems > increasingNum; increasingNum++)
{
     System.out.println("\nPlease enter item number: " + number);
     itemsArray[increasingNum] = keyboard.nextLine();
     number++;
}

number is not required, you can replace it with

for (int increasingNum = 0; increasingNum < numOfItems ; increasingNum++)
{
     System.out.println("\nPlease enter item number: " + (increasingNum + 1));
     itemsArray[increasingNum] = keyboard.nextLine();
}

and numOfItems != -1 && numOfItems < 0 is equivalent to just numOfItems < 0

Java Devil
  • 9,835
  • 7
  • 30
  • 44
  • Thanks Java Devil, but I'm not fully understanding what you're saying the issue is. I understood and fixed the unnecessary variables, but can you elaborate on what you initially said and/or send fixed code? i think I see what you're saying, but I don't see how a newline is affecting the keyboard.nextLine(). – noahj98 Jul 06 '17 at 03:52
  • See this [question and answer](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) for an explanation, your question is actually a duplicate of that one. – Java Devil Jul 06 '17 at 03:57
  • Not sure why this got downvoted... an explaination would be nice – Java Devil Jul 06 '17 at 04:34
-1

There are bunch of problems with your code, before you get the keyboard input you need to check for keyboard.hasNextInt() also you need to initialize the answer variable.

 public static void main(String[] args)

{

 int numOfItems = 0, answer = 0, number = 1, secondNumber = 1;
Scanner keyboard = new Scanner(System.in);

System.out.println("\n\nHello!\nThis is a program that will " +
                    "help you create a To-Do List.");

do
{
    System.out.println("\nHow many items do you want to add?");
    if(keyboard.hasNextInt())
    numOfItems = keyboard.nextInt();
}
while (numOfItems != -1 && numOfItems < 0);

String[] itemsArray = new String[numOfItems];

for (int increasingNum = 0; numOfItems > increasingNum; increasingNum++)
{
        System.out.println("\nPlease enter item number: " + number);
        itemsArray[increasingNum] = keyboard.nextLine();
        number++;
}

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");
if(keyboard.hasNextInt())
    answer = keyboard.nextInt();

if (answer == 1)
{
    for (int secondIncreasingNum = 0; numOfItems > secondIncreasingNum; secondIncreasingNum++)
    {
        System.out.println(secondNumber + ". " + itemsArray[secondIncreasingNum]);
        secondNumber++;
    }
}

System.exit(0);

} }

Arsaceus
  • 295
  • 2
  • 18