Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用两种方法打印数组列表_Java_Arraylist - Fatal编程技术网

Java 用两种方法打印数组列表

Java 用两种方法打印数组列表,java,arraylist,Java,Arraylist,我是一个新手程序员,需要一点帮助!我有一个家庭作业,在作业中我要倒转一个文件的行,也要倒转文件每行中单词的顺序。在我的解决方案中,我创建了两种方法的代码,但是,当我运行代码时,“reverseLine”方法会在控制台中打印出来,而“reverseWord”方法则不会。如果我先切换方法调用的顺序并调用reverseWord,则reverseLine不会打印 import java.util.Scanner; import java.util.ArrayList; import java.io.Fi

我是一个新手程序员,需要一点帮助!我有一个家庭作业,在作业中我要倒转一个文件的行,也要倒转文件每行中单词的顺序。在我的解决方案中,我创建了两种方法的代码,但是,当我运行代码时,“reverseLine”方法会在控制台中打印出来,而“reverseWord”方法则不会。如果我先切换方法调用的顺序并调用reverseWord,则reverseLine不会打印

import java.util.Scanner;
import java.util.ArrayList;
import java.io.File; 
import java.io.FileNotFoundException;
/*Write a program to reverse the lines of a file and also to reverse the order of the words in each line of the file. 
Use ArrayLists to help you. The program inputs the name of the file and writes the reversed output to standard out. 
In each line of output, the words printed out are separated by single spaces.*/
public class Homework3 {

   public static void main(String[] args) {
      try {
         Scanner scan = new Scanner(new File("input.txt"));
         reverseLine(scan);
         reverseWord(scan);
         scan.close();
      } catch (FileNotFoundException ex) {
         System.out.println("An error has occured.");
      }
   }

   public static void reverseLine(Scanner scan) { //Reverses the lines of the file
      ArrayList<String> line = new ArrayList<String>();
      while (scan.hasNextLine())  {
         line.add(scan.nextLine());
      }
      for(int i = line.size() - 1; i>=0; i--) {
         System.out.println(line.get(i));
      }
   }
    public static void reverseWord(Scanner scan) { //Reverses the words of the file
      ArrayList<String> word = new ArrayList<String>();
      while (scan.hasNext())  {
         word.add(scan.next());
      }
      for(int i = word.size() - 1; i>=0; i--) {
         System.out.print(word.get(i)+" ");
      }
   }
}
import java.util.Scanner;
导入java.util.ArrayList;
导入java.io.File;
导入java.io.FileNotFoundException;
/*编写一个程序来反转文件的行,并反转文件每行中单词的顺序。
使用ArrayList来帮助您。程序输入文件名并将反向输出写入标准输出。
在输出的每一行中,打印出来的单词用单个空格隔开*/
公开课家庭作业3{
公共静态void main(字符串[]args){
试一试{
扫描仪扫描=新扫描仪(新文件(“input.txt”);
反向线(扫描);
反向扫描;
scan.close();
}捕获(FileNotFoundException ex){
System.out.println(“发生错误”);
}
}
公共静态void reverseLine(扫描程序扫描){//反转文件的行
ArrayList行=新的ArrayList();
while(scan.hasNextLine()){
line.add(scan.nextLine());
}
对于(int i=line.size()-1;i>=0;i--){
System.out.println(line.get(i));
}
}
公共静态void reverseWord(扫描程序扫描){//反转文件的字
ArrayList word=新的ArrayList();
while(scan.hasNext()){
word.add(scan.next());
}
对于(int i=word.size()-1;i>=0;i--){
System.out.print(word.get(i)+”);
}
}
}
不太清楚问题出在哪里。感谢您的帮助

编辑——谢谢你的评论!我要试试你的建议

package com.dinesh.ritu.enterprises;
package com.dinesh.ritu.enterprises;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.io.File; 
import java.io.FileNotFoundException;
/*Write a program to reverse the lines of a file and also to reverse the order of the words in each line of the file. 
Use ArrayLists to help you. The program inputs the name of the file and writes the reversed output to standard out. 
In each line of output, the words printed out are separated by single spaces.*/
/*
 * I have modify your code , hope your problem got solved with these changes :: Happy Coding 
 */
public class Homework3 {

   public static void main(String[] args) {
      try {
         Scanner scan = new Scanner(new File("input.txt"));
        //You have to scan the file again as after completing the first task the file was closed , below line will help you to run both the method  
       Scanner scan1 = new Scanner(new File("input.txt")); 
         reverseLine(scan);
         reverseWord(scan1);
         scan.close();
      } catch (FileNotFoundException ex) {
         System.out.println("An error has occured.");
      }
   }

   public static void reverseLine(Scanner scan) { //Reverses the lines of the file
      ArrayList<String> line = new ArrayList<String>();
      while (scan.hasNextLine())  {
         line.add(scan.nextLine());
      }
      for(int i = line.size() - 1; i>=0; i--) {
         System.out.println(line.get(i));
      }
      System.out.println("---------"+"Method reverseLine Ended Successfully");
   }
    public static void reverseWord(Scanner scan) { //Reverses the words of the file
      ArrayList<String> word = new ArrayList<String>();
      while (scan.hasNext())  {
         word.add(scan.next());
      }
      //Added new code for reversing the words hope this is what you want ?? Else let me know 
      Collections.reverse(word);
      System.out.println(word);
  }
}
导入java.util.Scanner; 导入java.util.ArrayList; 导入java.util.Collection; 导入java.util.Collections; 导入java.io.File; 导入java.io.FileNotFoundException; /*编写一个程序来反转文件的行,并反转文件每行中单词的顺序。 使用ArrayList来帮助您。程序输入文件名并将反向输出写入标准输出。 在输出的每一行中,打印出来的单词用单个空格隔开*/ /* *我已经修改了你的代码,希望你的问题能通过这些修改得到解决::快乐编码 */ 公开课家庭作业3{ 公共静态void main(字符串[]args){ 试一试{ 扫描仪扫描=新扫描仪(新文件(“input.txt”); //您必须再次扫描该文件,因为在完成第一个任务后,该文件已关闭,下面的行将帮助您运行这两个方法 Scanner scan1=新扫描仪(新文件(“input.txt”); 反向线(扫描); 弗雷瑟沃德(scan1); scan.close(); }捕获(FileNotFoundException ex){ System.out.println(“发生错误”); } } 公共静态void reverseLine(扫描程序扫描){//反转文件的行 ArrayList行=新的ArrayList(); while(scan.hasNextLine()){ line.add(scan.nextLine()); } 对于(int i=line.size()-1;i>=0;i--){ System.out.println(line.get(i)); } System.out.println(“-----------”+“方法反向线成功结束”); } 公共静态void reverseWord(扫描程序扫描){//反转文件的字 ArrayList word=新的ArrayList(); while(scan.hasNext()){ word.add(scan.next()); } //添加了新的代码,用于反转“希望”这是您想要的??否则请告诉我 收藏。反面(字); System.out.println(word); } }
这是因为在第二种方法中,当扫描器迭代器已经在末尾时,它正在尝试遍历文件。其他提示:您的
reverseWord
方法可能会在一行上运行。感谢您的帮助,Dinesh!我想看看是否可以重置扫描仪,但您创建两个扫描仪的方式似乎更好!再次感谢你,伙计!