英文:
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<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();
}
}
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<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++){
System.out.println(" enter the " + i + " number : ");
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<String> cars=new ArrayList<String>();
Scanner sc =new Scanner (System.in);
System.out.println(" enter the size of the list ");
int size=sc.nextInt();
sc.nextLine();
for( int i =0;i<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<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
有几种方法可以解决这个问题,例如:
- 使用
Integer.parseInt(sc.nextLine())
输入大小。 - 在
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]
还有一些建议(如上面的代码所示):
- 使用泛型类型,即
List
,而不是ArrayList
,例如:List<String> cars=new ArrayList<>();
。另外,请注意你不需要在右侧再次使用<String>
;编译器可以从简单的<>
推断出它。 - 不要关闭用于
System.in
的Scanner
,因为它也会关闭System.in
,除非重新启动应用程序。在应用程序中可能会有多个类,并且应用程序中的其他某个类需要从键盘(System.in
)获取一些输入。
英文:
There are a couple of ways to deal with this problem e.g.
- Input the size using
Integer.parseInt(sc.nextLine())
- Put a
sc.nextLine();
afterint 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<String> cars = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the list: ");
int size = Integer.parseInt(sc.nextLine());
for (int i = 0; i < 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):
- Use the Generic type i.e.
List
instead ofArrayList
for the type of the variable e.g.List<String> cars=new ArrayList<>();
. Also, note that you do not need to put<String>
again on the right side; the compiler can infer it from simply<>
. - Do not close a
Scanner
forSystem.in
as it also closesSystem.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
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论