简体   繁体   中英

Using manual input to store either integers, doubles, or strings in a Comparable ArrayList

I'm trying to prompt the user for input to store either integers, doubles, or strings in a Comparable ArrayList. The problem is, I don't ask them to let the program know which it is. My solution was to parse the input for integers or doubles. If both of them return false, then I know it's a string. Here's my code:

int choice2num = 0;
        System.out.println("Please enter your values: (Press enter to stop) ");
        int integer = 0;
        double doubl = 0;
        boolean hasInt = true;
        boolean hasDouble = true;
        do
        {
            try
            {

                tempChoice = userIn2.nextLine();
                if (hasInt){
                    try{
                        integer = Integer.parseInt(tempChoice);
                    }catch (NumberFormatException e)
                    {
                        hasInt = false;
                    }
                }

                if (haveInt == false && hasDouble){
                    try { 
                        doubl = Double.parseDouble(tempChoice);
                    }catch (NumberFormatException er)
                    {
                        hasDouble = false;
                    }
                }

                if (hasInt)
                {
                    try{
                    integer = Integer.parseInt(tempChoice);
                    }catch (NumberFormatException e )
                    {
                        System.out.println("Wrong format, please try again.");
                        break;
                    }
                    list.add(integer);
                }

                if (hasDouble)
                {
                    try {
                    doubl = Double.parseDouble(tempChoice);
                    }catch (NumberFormatException e)
                    {
                        System.out.println("Wrong format, please try again.");
                        break;
                    }
                    list.add(doubl);
                }

                if (!hasDouble && !hasInt)
                {
                    list.add(tempChoice);
                }

                if (!tempChoice.equals(""))
                {
                    choice2num = Integer.parseInt(tempChoice);
                    list.add(choice2num);
                }
            }
            catch(NumberFormatException e)
            {
                System.out.println(e.getMessage());
            }
        } while (!tempChoice.equals(""));

        System.out.println("List:  " + list);
        secondMenu();
    }

It's pretty messy, I know. When I run the code in the program, and enter integers 1, 2, 3, 4, and 5, it returns this:

Wrong format, please try again.
List:  [1, 1.0, 1, 2, 2.0, 2, 3, 3.0, 3, 4, 4.0, 4, 5, 5.0, 5]

What is my error here?

You need an if else block.

if (userIn2.hasNextInt()){
  // process integer
  obj = userIn2.nextInt();
} else if(userIn2.hasNextDouble()) {
  // process double
  obj = userIn2.nextDouble();
} else {
  // process String
  obj = userIn2.next();
}

Use this order to get integers first, then doubles and lastly strings.

For one thing, if your input is an int then hasInt and hasDouble will both be true. In that case both blocks will run and the same number will be added as both an int and a double. (You're also not resetting them anywhere. If you enter something that isn't an int, no more ints will be added at all afterwards.)

Not only that, but in this block you're parsing the input and adding to the list a third time.

if (!tempChoice.equals(""))
    {
        choice2num = Integer.parseInt(tempChoice);
        list.add(choice2num);
    }

There's quite a few more problems here (you parse the string into an int or double twice) but the immediate cause of your issue is not using else if to avoid doing mutually exclusive things.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM