如何在Eclipse程序中重定向输入和输出

时间:2015-03-16 01:01:46

标签: java

我有一个我在Eclipse中创建的程序,它有一个控制台菜单(只是纯文本)并使用Scanner接受用户输入。我需要使其自动化,以便我可以通过以下方式从命令行运行程序:java MyProgramName< input.txt> output.txt的

我尝试将以下内容放在我的主要内容中:

try {
    System.setIn(new FileInputStream("input.txt"));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    System.out.println("input.txt does not exist!");
    e.printStackTrace();
}

但我得到以下例外:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)

有人知道这里有什么问题吗?

编辑:值得注意的是,我的程序有3个类,所有3个都使用Scanner实例。

这是我的半功能计划:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Map.Entry;

enum MONTHS
{
    January, February, March, April, May, June, July, August, September, October, November, December;
}
enum DAYS
{
    Su, Mo, Tu, We, Th, Fr, Sa;
}


public class MyCalendarTester implements java.io.Serializable {


    static MONTHS[] arrayOfMonths = MONTHS.values();
    static DAYS[] arrayOfDays = DAYS.values();
    static MyCalendar myCal = new MyCalendar();
    static Event myEvent = new Event(null, null, null);

    public static void main(String[] args) {
        // TODO Auto-generated method stub
//      try {
//          System.setIn(new FileInputStream("input.txt"));
//      } catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          System.out.println("input.txt does not exist!");
//          e.printStackTrace();
//      }

        Scanner in = null;
        try {
            in = new Scanner(new File("input.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        GregorianCalendar cal = new GregorianCalendar(); // capture today
        printCalendar(cal);
        menuHandler(in);

        System.out.println("\nI think we're done here!");
    }

    public static void  printCalendar(Calendar cal){

        GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
        dayCounter.set(Calendar.DAY_OF_MONTH, 1);

        int formatCounter = 0;

        System.out.println("    " + arrayOfMonths[cal.get(Calendar.MONTH) ] + " " + cal.get(Calendar.YEAR)); //prints the month and year

        for(int i = 0; i < arrayOfDays.length; i++){
            if(i == 0){
                System.out.print(arrayOfDays[i]);
            }
            else{
                System.out.print(" " + arrayOfDays[i]);
            }
        }//print days of week

        System.out.println();
        for(int i = 0; i < arrayOfDays.length; i++){
            if(!arrayOfDays[i].equals(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1])){
                System.out.print("   ");
                formatCounter++;
            }
            else{
                System.out.print(" " + cal.getActualMinimum(Calendar.DAY_OF_MONTH) + " ");
                formatCounter++;
                break;
            }
        }

        for(int i = 1; i < cal.getMaximum(Calendar.DAY_OF_MONTH); i++){
            if(formatCounter == 7){
                System.out.println();
                formatCounter = 0; //reset counter
            }
            dayCounter.roll(Calendar.DAY_OF_MONTH, true);
            if(dayCounter.get(Calendar.DATE) == cal.get(Calendar.DATE)){
                System.out.print("[" + dayCounter.get(Calendar.DATE) + "]");
                formatCounter++;
            }
            else if(dayCounter.get(Calendar.DATE) <= 9){
                System.out.print(" " + dayCounter.get(Calendar.DATE) + " ");
                formatCounter++;
            }
            else{
                System.out.print(dayCounter.get(Calendar.DATE) + " ");
                formatCounter++;
            }


        }
    }

public static void menuHandler(Scanner in){

System.out.println("\nSelect one of the following options:");
System.out.println("[L]oad [V]iew by [C]reate [G]o to [E]vent list [D]elete [Q]uit");

        char userChoice = in.next().charAt(0);
        switch(Character.toUpperCase(userChoice)){
            case 'L':
                    MyCalendar.load();
                    menuHandler(in);
                    break;

            case 'V': 
                     myCal.viewCalendar(in);
                     menuHandler(in);
                     break;

            case 'C':
                     myEvent.createEvent(in);
                     menuHandler(in);
                     break;

            case 'G':
                    myCal.goTo(in);
                    menuHandler(in);
                    break;
            case 'E':
                    myEvent.eventList(in);
                    menuHandler(in);
                    break;

            case 'D':
                    myEvent.deleteEvent(in);
                    menuHandler(in);
                    break;

            case 'Q': MyCalendar.save();
                        break;

        }

        return;

    }




}

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Map.Entry;


public class MyCalendar implements java.io.Serializable{

    GregorianCalendar calendar;
    private static HashMap<GregorianCalendar, Event> myCalHash;
    GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
    static MONTHS[] arrayOfMonths = MONTHS.values();
    static DAYS[] arrayOfDays = DAYS.values();

    MyCalendar(){
        calendar = new GregorianCalendar();
        myCalHash = new HashMap<GregorianCalendar, Event>();
    }

    public HashMap<GregorianCalendar, Event> getMyCalHash(){
        return myCalHash;
    }

    public void setMyCalHash(HashMap<GregorianCalendar, Event> myCalHash) {
        this.myCalHash = myCalHash;
    }

    public void viewCalendar(Scanner in) {

            System.out.print("[D]ay view or [M]view? ");
            char userChoice = in.next().charAt(0);
            if(Character.toUpperCase(userChoice) == 'D'){ dayView(in); }
            else if(Character.toUpperCase(userChoice) == 'M'){ monthView(in); }
            else{
                System.out.println("Invalid choice.");
            }
    }   

        public void dayView(Scanner in){
            //print day calendar
            try {
                System.setIn(new FileInputStream("input.txt"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("input.txt does not exist!");
                e.printStackTrace();
            }

            ArrayList<Event> tempArr = new ArrayList<Event>();
            System.out.println(arrayOfDays[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " +  arrayOfMonths[calendar.get(Calendar.MONTH)] 
                    + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
               for (Entry<GregorianCalendar, Event> entry : MyCalendarTester.myCal.getMyCalHash().entrySet()) {

                    if(entry.getKey().get(Calendar.MONTH) == (calendar.get(Calendar.MONTH) + 1)
                            && entry.getKey().get(Calendar.DATE) == calendar.get(Calendar.DATE)
                            && entry.getKey().get(Calendar.YEAR) == calendar.get(Calendar.YEAR)){
                        tempArr.add(entry.getValue());

                    }
               }
                    if(tempArr.size() > 0){
                        Collections.sort(tempArr);
                    }
                    for(Event e: tempArr){
                        if(e.endTime != null){
                            System.out.println("    " + e.eventName + " " + e.startTime.get(Calendar.HOUR_OF_DAY) + ":" + 
                            e.startTime.get(Calendar.MINUTE) + " " + e.endTime.get(Calendar.HOUR_OF_DAY) 
                            + ":" + e.endTime.get(Calendar.MINUTE));
                        }
                        else{
                            System.out.println(e.eventName + " " + e.startTime.get(Calendar.HOUR_OF_DAY) + ":" + 
                                    e.startTime.get(Calendar.MINUTE));
                        }
                    }



            System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
            char userChoice = in.next().charAt(0);
            if(Character.toUpperCase(userChoice) == 'P'){
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                dayView(in);
            }
            else if(Character.toUpperCase(userChoice) == 'N'){
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dayView(in);
            }
            else if(Character.toUpperCase(userChoice) == 'M'){
                return;
            }
            else{
                System.out.println("Invalid choice.");
                return;
            }
        }
        public void monthView(Scanner in){
            //print month calendar

            int formatCounter = 0;
            boolean eventDay = false;
            dayCounter.set(Calendar.DAY_OF_MONTH, 1);

            System.out.println("    " + arrayOfMonths[calendar.get(Calendar.MONTH) ] + " " + calendar.get(Calendar.YEAR)); //prints the month and year

            for(int i = 0; i < arrayOfDays.length; i++){
                if(i == 0){
                    System.out.print(arrayOfDays[i]);
                }
                else{
                    System.out.print(" " + arrayOfDays[i]);
                }
            }//print days of week

            System.out.println();
            for(int i = 0; i < arrayOfDays.length; i++){
                if(!arrayOfDays[i].equals(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1])){
                    System.out.print("   ");
                    formatCounter++;
                }
                else{
                    for (Entry<GregorianCalendar, Event> entry : MyCalendarTester.myCal.getMyCalHash().entrySet()) {
//                      System.out.println("entry.getKey().get(Calendar.MONTH) = " + (entry.getKey().get(Calendar.MONTH)) );
//                      System.out.println(" (dayCounter.get(Calendar.MONTH) + 1)  = " + (dayCounter.get(Calendar.MONTH) + 1) );
//                      System.out.println("entry.getKey().get(Calendar.DATE) = " + entry.getKey().get(Calendar.DATE));
//                      System.out.println("(dayCounter.get(Calendar.DATE) = " + (dayCounter.get(Calendar.DATE)));
                        if(entry.getKey().get(Calendar.MONTH) == (dayCounter.get(Calendar.MONTH) + 1)
                                && entry.getKey().get(Calendar.DATE) == (dayCounter.get(Calendar.DATE))
                                && entry.getKey().get(Calendar.YEAR) == dayCounter.get(Calendar.YEAR)){
                                    System.out.print("[" + calendar.getActualMinimum(Calendar.DAY_OF_MONTH) + "]");
                                    formatCounter++;
                                    eventDay = true;
                                    break;
                        }
                    }
                        if(!eventDay){
                            System.out.print(" " + calendar.getActualMinimum(Calendar.DAY_OF_MONTH) + " ");
                            formatCounter++;
                            break;
                        }

                    break;
                }

            }


            for(int i = 1; i < dayCounter.getActualMaximum(Calendar.DAY_OF_MONTH); i++){
                if(formatCounter == 7){
                    System.out.println();
                    formatCounter = 0; //reset counter
                }
                eventDay = false;
                dayCounter.roll(Calendar.DAY_OF_MONTH, true);
                for (Entry<GregorianCalendar, Event> entry : MyCalendarTester.myCal.getMyCalHash().entrySet()) {    
                    if(entry.getKey().get(Calendar.MONTH) == (dayCounter.get(Calendar.MONTH) + 1)
                            && entry.getKey().get(Calendar.DATE) == (dayCounter.get(Calendar.DATE))
                            && entry.getKey().get(Calendar.YEAR) == dayCounter.get(Calendar.YEAR)){
                            System.out.print("[" + dayCounter.get(Calendar.DATE) + "]");
                            formatCounter++;
                            eventDay = true;
                            break;

                    }
                }
                if(!eventDay){
                    if(dayCounter.get(Calendar.DATE) <= 9){
                        System.out.print(" " + dayCounter.get(Calendar.DATE) + " ");
                        formatCounter++;
                    }
                    else{
                        System.out.print(dayCounter.get(Calendar.DATE) + " ");
                        formatCounter++;
                    }
                }


            }

            System.out.print("\n[P]revious or [N]ext or [M]ain menu ? ");
            char userChoice = in.next().charAt(0);
            if(Character.toUpperCase(userChoice) == 'P'){
                calendar.add(Calendar.MONTH, -1);
                dayCounter.add(Calendar.MONTH, -1);
                monthView(in);
            }
            else if(Character.toUpperCase(userChoice) == 'N'){
                calendar.add(Calendar.MONTH, 1);
                dayCounter.add(Calendar.MONTH, 1);
                monthView(in);
            }
            else if(Character.toUpperCase(userChoice) == 'M'){
                calendar = (GregorianCalendar) Calendar.getInstance(); //resets the calendar back to the current month
                dayCounter = (GregorianCalendar) Calendar.getInstance(); //resets the calendar back to the current month
                return;
            }
            else{
                System.out.println("Invalid choice.");
                return;
            }
        }

        public void goTo(Scanner in){

            System.out.print("Enter a date in MM/DD/YYYY format: ");

            String userDate = in.nextLine();
            String[] dateArr = userDate.split("/");
               dayCounter.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
               dayCounter.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]) - 1);
               dayCounter.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));

             System.out.println(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1] + ", " +  
             arrayOfMonths[dayCounter.get(Calendar.MONTH)] + " " + dayCounter.get(Calendar.DATE) + ", " + dayCounter.get(Calendar.YEAR));

               for (Entry<GregorianCalendar, Event> entry : MyCalendarTester.myCal.getMyCalHash().entrySet()) {
                    if(entry.getKey().get(Calendar.MONTH) == Integer.parseInt(dateArr[0]) 
                            && entry.getKey().get(Calendar.DATE) == Integer.parseInt(dateArr[1]) 
                            && entry.getKey().get(Calendar.YEAR) == Integer.parseInt(dateArr[2]) ){
                        Event value = entry.getValue();

                        if(value.endTime != null){
                            System.out.println(value.eventName + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + 
                                    value.startTime.get(Calendar.MINUTE) + " " + value.endTime.get(Calendar.HOUR_OF_DAY) 
                                    + ":" + value.endTime.get(Calendar.MINUTE));
                        }else{
                            System.out.println(value.eventName + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + 
                                    value.startTime.get(Calendar.MINUTE));

                        }

                    }
               }

        }
        public static void save(){

            SortedSet<GregorianCalendar> keys = new TreeSet<GregorianCalendar>(MyCalendarTester.myCal.getMyCalHash().keySet());
               try
                  {
                     FileOutputStream fileOut = new FileOutputStream("events.txt");
                     ObjectOutputStream out = new ObjectOutputStream(fileOut);
                     out.writeObject(myCalHash);
                     out.close();
                     fileOut.close();
                     System.out.printf("Serialized data is saved in events.txt");
                  }catch(IOException i)
                  {
                      i.printStackTrace();
                  }
        }

        public static void load(){

              try
              {
                 FileInputStream fileIn = new FileInputStream("events.txt");
                 ObjectInputStream in = new ObjectInputStream(fileIn);
                 myCalHash = (HashMap<GregorianCalendar, Event>) in.readObject();
                 in.close();
                 fileIn.close();
              }catch(IOException i)
              {
                 System.out.println("File not found. You must be running the program for the first time.");
                 return;
              }catch(ClassNotFoundException c)
              {
                 System.out.println("Employee class not found");
                 c.printStackTrace();
                 return;
              }

        }
}



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.xml.soap.Node;


public class Event implements Comparable, java.io.Serializable {


    String eventName;
    GregorianCalendar startTime;
    GregorianCalendar endTime;

    MyCalendarTester mCal = new MyCalendarTester();

    Event(String eventName, GregorianCalendar startTime, GregorianCalendar endTime){
        this.eventName = eventName;
        this.startTime = startTime;
        this.endTime = endTime;
    } //end Event Constructor

    public GregorianCalendar getStartTime(){
        return startTime;
    }
    public void createEvent(Scanner in){

//      Scanner sc = new Scanner(System.in);

        System.out.print("Enter the name of the event: ");
        String name = in.nextLine();
        System.out.print("Date of event: ");
        String dateAsString = in.nextLine();
        System.out.println("dateASString = " + dateAsString);
        String startTime = null;
        String endTime = null;
        GregorianCalendar calDate = new GregorianCalendar();
        String[] dateArr = dateAsString.split("/");
           calDate.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
           calDate.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]));
           calDate.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));

        System.out.print("Enter starting time and ending time(24 hour format, comma separated):");
        String times = in.nextLine();
        // checks if user entered both start and end time
        if(times.contains(",")){
            String[] time = times.split(",");
            startTime = time[0];
            endTime = time[1];

            GregorianCalendar calSTime = new GregorianCalendar();
            String[] sTime = startTime.split(":");
            calSTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sTime[0]));
            calSTime.set(Calendar.MINUTE, Integer.parseInt(sTime[1]));


            GregorianCalendar calETime = new GregorianCalendar();
            String[] eTime = endTime.split(":");
            calETime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(eTime[0]));
            calETime.set(Calendar.MINUTE, Integer.parseInt(eTime[1]));


            Event myEvent = new Event(name, calSTime, calETime); 


            MyCalendarTester.myCal.getMyCalHash().put(calDate,  myEvent); //adding date and event to HashMap

        }else{ //if user did not specify an event end time
            startTime = times;
            endTime = null;

            GregorianCalendar calSTime = new GregorianCalendar();
            String[] sTime = startTime.split(":");
            calSTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sTime[0]));
            calSTime.set(Calendar.MINUTE, Integer.parseInt(sTime[1]));
            Event myEvent = new Event(name, calSTime, null);
            MyCalendarTester.myCal.getMyCalHash().put(calDate,  myEvent);//adding date and event to HashMap
        }




    }

    public void deleteEvent(Scanner in){


    //Scanner sc = new Scanner(System.in);
        System.out.println("[S]elected or [A]ll ?");
        char userChoice = in.next().charAt(0);
        if(Character.toUpperCase(userChoice) == 'S'){ 
            //delete all events on the day

            System.out.println("Enter the date: ");

            String deleteDate = in.nextLine();

                String[] parsed = deleteDate.split("/");
                System.out.println("MyCalendarTester.myCal.getMyCalHash().size() = " + MyCalendarTester.myCal.getMyCalHash().size());

                SortedSet<GregorianCalendar> keys = new TreeSet<GregorianCalendar>(MyCalendarTester.myCal.getMyCalHash().keySet());

                for (GregorianCalendar entry :  keys) {
                    GregorianCalendar copyEntry = entry;
                    if(entry.get(Calendar.MONTH) == Integer.parseInt(parsed[0]) 
                            && entry.get(Calendar.DATE) == Integer.parseInt(parsed[1]) 
                            && entry.get(Calendar.YEAR) == Integer.parseInt(parsed[2]) ){
                                MyCalendarTester.myCal.getMyCalHash().remove(copyEntry);

                    }

                }

        }
        else if(Character.toUpperCase(userChoice) == 'A'){ 
            MyCalendarTester.myCal.getMyCalHash().clear();
        }
        else{
            System.out.println("Invalid choice.");
        }
    }

public void eventList(Scanner in){

        int year = -1;
        GregorianCalendar tempKey = null;
        if(MyCalendarTester.myCal.getMyCalHash().equals(null)){
            System.out.println("Your calendar is empty!");
        }
        else{
            System.out.println("Here are your events: ");
            SortedSet<GregorianCalendar> keys = new TreeSet<GregorianCalendar>(MyCalendarTester.myCal.getMyCalHash().keySet());

            Set<Entry<GregorianCalendar, Event>> map = (MyCalendarTester.myCal.getMyCalHash().entrySet()); 

            List<Map.Entry> list = new LinkedList<Map.Entry>(map);

//          Collections.sort(list, new Comparator<Map.Entry<GregorianCalendar, Event>>() {
//              public int compare(Map.Entry<GregorianCalendar, Event> e1, Map.Entry<GregorianCalendar,Event> e2) {
//                  
//                  return ( e1.getValue()).compareTo(e2.getValue());
//             
//              }
//          });
//          Iterator i2 = list.iterator();
//          while (i2.hasNext()) {
//              System.out.print(i2.next() + " , ");
//          }
//          
            for(GregorianCalendar key : keys){
                Event value = MyCalendarTester.myCal.getMyCalHash().get(key);
                tempKey = key;
                if(value.endTime != null){
                    if(key.get(Calendar.YEAR) == year){

                        System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
                                + key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " - " + value.endTime.get(Calendar.HOUR_OF_DAY) 
                                + ":" + value.endTime.get(Calendar.MINUTE) + " " + value.eventName);

                    }else{
                        System.out.println(key.get(Calendar.YEAR));
                        System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
                                + key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " - " + value.endTime.get(Calendar.HOUR_OF_DAY) 
                                + ":" + value.endTime.get(Calendar.MINUTE) + " " + value.eventName );
                        year = key.get(Calendar.YEAR);
                    }

                }else{
                    if(key.get(Calendar.YEAR) == year){
                        System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
                                + key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " " + value.eventName);

                    }else{
                        System.out.println(key.get(Calendar.YEAR));
                        System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
                                + key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE)  + " " + value.eventName);
                        System.out.println();
                        year = key.get(Calendar.YEAR);
                    }
                }

            }

        }

    }
@Override
    public int compareTo(Object e) {
        // TODO Auto-generated method stub
    if(((Event) e).mCal.myCal.getMyCalHash().get(Calendar.MONTH) == (this.mCal.myCal.getMyCalHash().get(Calendar.MONTH))
            && ((Event) e).mCal.myCal.getMyCalHash().get(Calendar.DATE) == (this.mCal.myCal.getMyCalHash().get(Calendar.DATE))
            && ((Event) e).mCal.myCal.getMyCalHash().get(Calendar.YEAR) == (this.mCal.myCal.getMyCalHash().get(Calendar.YEAR))){

       int hour = ((Event) e).getStartTime().get(Calendar.HOUR_OF_DAY);
       int minute = ((Event) e).getStartTime().get(Calendar.MINUTE);
       int anotherHour = this.startTime.get(Calendar.HOUR_OF_DAY);
       int anotherMinute = this.startTime.get(Calendar.MINUTE);

       if(anotherHour - hour == 0 ){
           return anotherMinute - minute;
       }else{
           return anotherHour - hour;
       }
    }

    return 0;

    }
}



//  
//static <GregorianCalendar,Event extends Comparable<? super Event>> 
//List<Entry<GregorianCalendar, Event>> entriesSortedByValues(Map<GregorianCalendar,Event> map) {
//
//List<Entry<GregorianCalendar,Event>> sortedEntries = new ArrayList<Entry<GregorianCalendar,Event>>(map.entrySet());
//
//Collections.sort(sortedEntries, 
//new Comparator<Entry<GregorianCalendar,Event>>() {
//    @Override
//    public int compare(Entry<GregorianCalendar,Event> e1, Entry<GregorianCalendar,Event> e2) {
//        return e2.getValue().compareTo(e1.getValue());
//    }
//}
//);
//
//return sortedEntries;
//}
//
//}

1 个答案:

答案 0 :(得分:0)

继续使用Scanner但使用此构造函数

Scanner in = new Scanner(new File("input.txt"));