如何接受用户输入到两个并行数组中

时间:2019-04-07 00:59:42

标签: java java.util.scanner parallel-arrays

我需要接受用户的String输入和double输入,并将它们存储到两个并行数组中以创建购物车。

我已将两个数组初始化为用户确定的大小Amount,并创建了一个for循环以运行输入操作并将值存储在两个数组中,并设置了一个额外的input.nextLine(),因此代码将吞下在循环末尾创建的新行。到目前为止,这是我的代码,

import java.util.*;
import java.io.*;
public class Item
{
    public static void main(String[] args)
    throws java.io.IOException
    {
        int amount;
        String nextItem,I;

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the number of items you would like to add:");
        amount = input.nextInt();

        String cart[] = new String[amount];
        double price[] = new double[amount];

        for(int i = 0; i<amount; i++)
        {
            System.out.println("Please enter the name of an item:");
            cart[i] = input.nextLine();

            System.out.println("Price?");
            price[i] = input.nextDouble();
            input.nextLine();

        }

     }
}

但是,当程序运行循环时,它将同时运行两个System.out.println命令,跳过第一个input.nextLine(),并且仅接受整数输入。

1 个答案:

答案 0 :(得分:0)

使用两个扫描器类

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 27, 2021 - 12:25:24
Django version 2.2.10, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /edit/update/1
Traceback (most recent call last):
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/app/views.py", line 95, in update
    member.save()
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/base.py", line 740, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/base.py", line 777, in save_base
    updated = self._save_table(
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/base.py", line 850, in _save_table
    updated = self._do_update(base_qs, using, pk_val, values, update_fields,
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/base.py", line 900, in _do_update
    return filtered._update(values) > 0
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/query.py", line 760, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1462, in execute_sql
    cursor = super().execute_sql(result_type)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1120, in execute_sql
    sql, params = self.as_sql()
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1428, in as_sql
    val = field.get_db_prep_save(val, connection=self.connection)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 793, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1279, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1274, in get_prep_value
    return self.to_python(value)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1236, in to_python
    parsed = parse_date(value)
  File "/home/danish-khan/django_drf/django-dashboard-gradientable/env/lib/python3.8/site-packages/django/utils/dateparse.py", line 74, in parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object
[27/Feb/2021 12:25:26] "GET /edit/update/1 HTTP/1.1" 500 139040

// 使用两个扫描器类