Java 需要帮助从扫描器获取下一行并将值分配给位置i处的数组元素吗
我对编程非常陌生,我很难理解如何编写这行代码,我必须从扫描器中读取下一行代码,并将值分配给位置I处的烛光。我还被告知,我需要合并readLine()以使输入光标前进到第一行Java 需要帮助从扫描器获取下一行并将值分配给位置i处的数组元素吗,java,arrays,Java,Arrays,我对编程非常陌生,我很难理解如何编写这行代码,我必须从扫描器中读取下一行代码,并将值分配给位置I处的烛光。我还被告知,我需要合并readLine()以使输入光标前进到第一行 public static void main(String[] args) throws FileNotFoundException { // File containing candy names File f = new File("candy.txt"); Scanner sc = n
public static void main(String[] args) throws FileNotFoundException {
// File containing candy names
File f = new File("candy.txt");
Scanner sc = new Scanner(f);
// Array of candy names
String[] candyNames;
// Number of names
int numNames = sc.nextInt();
// Move to the next line
sc.nextLine();
// Set the array size
candyNames = new String[numNames];
// Read each line and copy to array element
for (int i = 0; i < numNames; i++) {
// Add the missing statement
//The statement should get the next line from the scanner and assign
//the value to the candyArray element at position i.
candyNames[i].readLine(numNames);
System.out.println("Adding " + candyNames[i]);
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
//包含糖果名称的文件
文件f=新文件(“candy.txt”);
扫描仪sc=新扫描仪(f);
//糖果名称数组
字符串[]candyNames;
//姓名数目
int numNames=sc.nextInt();
//移到下一行
sc.nextLine();
//设置数组大小
candyNames=新字符串[numNames];
//读取每一行并复制到数组元素
对于(int i=0;i
字符串类中没有readLine()
方法。将candyNames[i].readLine(numNames)
替换为candyNames[i]=sc.nextLine()
sc.nextLine()
它将返回一个字符串。您需要将其分配给某个对象,就像分配常规字符串一样
candyNames[i] = "test";
// or
candyNames[i] = sc.nextLine();
然后代码的其余部分应该可以工作,因为您在print语句中将candyNames[i]用作字符串。只需执行candyNames[i]=sc.nextLine();
字符串类中没有readLine()
方法