有没有办法解决我遇到的错误?

huangapple go评论72阅读模式
英文:

Is there a way to remove the error that i am getting?

问题

import java.util.ArrayList;
import java.util.*;

public class Solution {
    public static void main (String args []){
        ArrayList<String> cars = new ArrayList<String>();

        Scanner sc = new Scanner (System.in);
        System.out.println(" enter the size of the list ");
        int size = sc.nextInt();
        
        for (int i = 0; i < size; i++){ 
            cars.add(i, sc.nextLine());
        }
        System.out.println(cars);
        sc.close();
    }
}

这个程序应该输出:

1,2,3,4

但是如果我输入大小为4,并且只输入这些值,但是实际上,如果我将大小写成4... 它不会从我这里获取4个参数,而是会获取三个参数并打印出('',1,2,3)。

请帮忙检查。

英文:
import java.util.ArrayList;
import java.util.*;

public class Solution { 
    public static void main (String args []){
        ArrayList&lt;String&gt; cars=new ArrayList&lt;String&gt;();

        Scanner sc =new Scanner (System.in);
        System.out.println(&quot; enter the size of the list &quot;);
        int size=sc.nextInt();
        

        for( int i =0;i&lt;size;i++){ 
             
             cars.add(i,sc.nextLine());
     
        }
        System.out.println(cars);
        sc.close();
    }
}

This program should output

1,2,3,4

But if I enter the size as 4 and I enter these values only but instead, if I write the size as 4 ... it doesn't take 4 arguments from me instead it will take three and print(' ',1,2,3)

please help

答案1

得分: 2

nextLine() 方法将指针移动到下一行因此使用 next 方法可以解决这个问题
import java.util.ArrayList;
import java.util.*;

public class Solution {
    public static void main(String args[]) {
        ArrayList<String> cars = new ArrayList<String>();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入列表的大小:");
        int size = sc.nextInt();

        for (int i = 0; i < size; i++) {
            System.out.println("请输入第 " + i + " 个数:");
            cars.add(i, sc.next());
        }
        System.out.println(cars);
        sc.close();
    }
}
英文:

The nextLine() traverse the pointer to the next line. So using next solves the problem

import java.util.ArrayList;

import java.util.*;
public class Solution { 
    public static void main (String args []){
       ArrayList&lt;String&gt; cars=new ArrayList&lt;String&gt;();

       Scanner sc =new Scanner (System.in);
        System.out.println(&quot; enter the size of the list &quot;);
        int size=sc.nextInt();
        

        for( int i =0;i&lt;size;i++){ 
            System.out.println(&quot; enter the &quot; + i + &quot; number : &quot;);
	        cars.add(i, sc.next());
     
        }
        System.out.println(cars);
        sc.close();
    }
}

答案2

得分: 2

你需要在 for 循环之前添加 sc.nextLine()

当你输入一个数字然后按下 Enter 键时,input.nextInt() 只会获取数字,而不会获取行尾的换行符。然而,当你执行 input.nextLine() 时,它会获取行尾的换行符,这个换行符已经在第一个输入中被缓存了。在使用 input.nextInt() 后立即使用 input.nextLine()

你试图接受一个数字,但你将 ArrayList 的类型声明为 String。你的代码会像这样:

import java.util.ArrayList;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> cars = new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        System.out.println("输入列表的大小:");
        int size = sc.nextInt();
        
        sc.nextLine();
        for (int i = 0; i < size; i++) {
            cars.add(i, sc.nextLine());
        }
        System.out.println(cars);
        sc.close();
    }
}
英文:

You need to add sc.nextLine() just before the for loop.

When you enter a number then press Enter, input.nextInt() takes only the number, not the end of the line. However, when you execute input.nextLine(), it takes the end of the line which is already in the buffer from the first input. Always use input.nextLine() immediately after input.nextInt()

You were trying to accept a number but you have declared the ArrayList type as String. Your code will look like this

import java.util.ArrayList;

import java.util.*;
public class Main
{
	public static void main(String[] args) {
	
        ArrayList&lt;String&gt; cars=new ArrayList&lt;String&gt;();
        Scanner sc =new Scanner (System.in);
        System.out.println(&quot; enter the size of the list &quot;);
        int size=sc.nextInt();
        
        sc.nextLine();
        for( int i =0;i&lt;size;i++){ 
             cars.add(i,sc.nextLine());
        }
        System.out.println(cars);
        sc.close();
	}
}

答案3

得分: 1

当你使用 list.add(i, value) 时,它的作用是将 value 保留在 List 的索引 i 处。
.nextLine 的作用是读取下一行,该行是空白的,从而导致在 list 的第一个位置上出现 *space*
因此,当你执行 cars.add(i, sc.nextLine()) 时,这将在第一个索引即 0 处添加空白。
你可以这样做:

for (int i = 0; i < size; i++) {
    cars.add(i, sc.next());
}

当你使用 sc.next() 时,它会检查下一个字符,而不是遍历到下一行。

英文:

when you do list.add(i, value) what it does is keeps the value in the i index of the of List.<br>
What .nextLine does it read the next line which which is blank, which resulting in *space* in the first position of the list. <br>
so when you are doing cars.add(i,sc.nextLine()) this is adding space in the first index ie 0.
What u can do is.

for( int i =0;i&lt;size;i++){ 
	  cars.add(i,sc.next());
	}

When you do sc.next() it check for the next character rather then Traversing to the nextLine.

答案4

得分: 1

有几种方法可以解决这个问题,例如:

  1. 使用 Integer.parseInt(sc.nextLine()) 输入大小。
  2. int size=sc.nextInt(); 后面加上 sc.nextLine();

这样做的原因是,Scanner#nextInt 不会消耗通过按下 <kbd>Enter</kbd> 输入的换行符。而 sc.nextLine() 会消耗这个换行符。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 声明列表
        List<String> cars = new ArrayList<>();

        Scanner sc = new Scanner(System.in);

        System.out.print("输入列表的大小: ");
        int size = Integer.parseInt(sc.nextLine());

        for (int i = 0; i < size; i++) {
            cars.add(i, sc.nextLine());
        }

        System.out.println(cars);
    }
}

一个样例运行:

输入列表的大小: 4
1
2
3
4
[1, 2, 3, 4]

还有一些建议(如上面的代码所示):

  1. 使用泛型类型,即 List,而不是 ArrayList,例如:List<String> cars=new ArrayList<>();。另外,请注意你不需要在右侧再次使用 &lt;String&gt;;编译器可以从简单的 &lt;&gt; 推断出它。
  2. 不要关闭用于 System.inScanner,因为它也会关闭 System.in,除非重新启动应用程序。在应用程序中可能会有多个类,并且应用程序中的其他某个类需要从键盘(System.in)获取一些输入。
英文:

There are a couple of ways to deal with this problem e.g.

  1. Input the size using Integer.parseInt(sc.nextLine())
  2. Put a sc.nextLine(); after int size=sc.nextInt();

The reason for this is that Scanner#nextInt does not consume the line-break which was entered by pressing <kbd>Enter</kbd>. The sc.nextLine() consumes this line-break.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// Declare the list
		List&lt;String&gt; cars = new ArrayList&lt;&gt;();

		Scanner sc = new Scanner(System.in);

		System.out.print(&quot;Enter the size of the list: &quot;);
		int size = Integer.parseInt(sc.nextLine());

		for (int i = 0; i &lt; size; i++) {
			cars.add(i, sc.nextLine());
		}

		System.out.println(cars);
	}
}

A sample run:

Enter the size of the list: 4
1
2
3
4
[1, 2, 3, 4]

A couple of more suggestions (as you can see in the code above):

  1. Use the Generic type i.e. List instead of ArrayList for the type of the variable e.g. List&lt;String&gt; cars=new ArrayList&lt;&gt;();. Also, note that you do not need to put &lt;String&gt; again on the right side; the compiler can infer it from simply &lt;&gt;.
  2. Do not close a Scanner for System.in as it also closes System.in and you will not be able to open it again unless you restart the application. There may be cases that your application has many classes and some other class in the application requires taking some input from the keyboard (System.in).

huangapple
  • 本文由 发表于 2020年9月27日 13:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64085117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定