This discussion is archived
3 Replies Latest reply: Apr 18, 2008 6:33 PM by 796440 RSS

Data input not working - String ... ... .nextLine(); - Very simple program.

807591 Newbie
Currently Being Moderated
I pasted the code below. It's fairly self explanatory. I want to be able to enter data of String and store it in the ArrayList. The
program essentially skips over this step but allows the user to enter the double in the very next part. Regardless of the position
of this element, the code skips over it not allowing data entry. I put it after the next part where you enter a double, and the double
still worked as supposed to but the String still did not. Help?
import java.util.ArrayList;
import java.util.Scanner;
public class CustomerTest
{
    public static void main(String[] args)
    {
        Scanner myScanner = new Scanner(System.in);
        ArrayList<Double> myList = new ArrayList<Double>();
        ArrayList<String> myList2 = new ArrayList<String>();
        boolean done = false;
        do
        {
            System.out.println("1 - Add a new customer");
            System.out.println("2 - Print all customers (names and sales)");
            System.out.println("3 - Compute and print the total sales");
            System.out.println("4 - Quit");
            double sInput = myScanner.nextDouble();
            if (sInput == 1)
            {
                System.out.print("Enter customer name: ");
                String sName = myScanner.nextLine();           //This is where I should be able to input String data, but it skips
                myList2.add(sName);                                    //^^ It won't allow input data no matter where I put this in the code
                System.out.print("Enter customer sales: ");
                double sSales = myScanner.nextDouble();
                myList.add(sSales);
            }
            else if (sInput == 2)
            {
                for (int i = 0; i < myList.size(); i++)
                {
                    System.out.println("Customer: " +myList2.get(i)+ " Sales: " +myList.get(i));
                }
            }
            else if (sInput == 3)
            {
                double total = 0;
                for (int i = 0; i < myList.size(); i++)
                {
                    total = total + myList.get(i);
                }
                System.out.println(total);
            }
            else if (sInput == 4)
            {
                done = true;
                System.out.println("Goodbye.");
            }
            else
            {
                System.out.println ("Invalid Option. Try Again");
            }
        }while (!done);
        System.exit(0);
    }
}
  • 1. Re: Data input not working - String ... ... .nextLine(); - Very simple program.
    796440 Guru
    Currently Being Moderated
    nextDouble, nextInt, etc., and, I think, next, do not consume the end of line.

    If your input is
    123.456
    Hello!
    987.654
    Goodbye!
    Then you'd do this:
    double d1 = scanner.nextDouble(); // 123.456, BUT leaves the EOL character(s)
    scanner.nextLine(); // consume the EOL after 123.456
    String s1 = scanner.nextLine(); // Hello!
    double d2 = scanner.nextDoubl(); // 987.654, but not EOL
    scanner.nextLine();
    String s2 = scanner.nextLine(); // Goodbye!
  • 2. Re: Data input not working - String ... ... .nextLine(); - Very simple program.
    807591 Newbie
    Currently Being Moderated
    Wow. Thanks so much. I'm not 100% sure on why it didn't work the way I had it as I've written a couple simple programs before
    and they executed perfectly and were written just like this one. It didn't involve any if statements, but it accepted data input and was written the same. If you have the chance to explain more, I'd be very greatful, but if not, it's ok and thanks again for the help.
  • 3. Re: Data input not working - String ... ... .nextLine(); - Very simple program.
    796440 Guru
    Currently Being Moderated
    jclubzzt wrote:
    Wow. Thanks so much.
    You're quite welcome.
    I'm not 100% sure on why it didn't work the way I had it as I've written a couple simple programs before
    and they executed perfectly and were written just like this one.
    No, they weren't. If they were, they'd've behaved the same way.
    It didn't involve any if statements,
    Irrelevant.
    If you have the chance to explain more, I'd be very greatful, but if not, it's ok and thanks again for the help.
    I don't know what else I can explain. NextDouble, etc., only consume enough characters until the end of, for instance, the double. There's still a newline character waiting to be gobbled up. NextLine consumes everything from the current position up through the newline character that ends the line.