以两种方法打印出数组列表

时间:2018-02-09 02:17:12

标签: java arraylist

我是一名需要一点帮助的新手程序员!我有一个家庭作业,我要反转文件的行,也要反转文件每行中单词的顺序。在我的解决方案中,我创建了两个代码,但是,当我运行代码时,“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)+" ");
      }
   }
}

不太确定问题是什么。任何帮助表示赞赏!

编辑 - 感谢您的评论!我打算试试你的建议!

1 个答案:

答案 0 :(得分:0)

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);
  }
}