在Java中获取开始和结束日期

时间:2013-04-19 00:32:07

标签: java

我正在用Java编写一个程序,允许用户选择一个选项(1:New,2:Print Range,3:Print All,quit)。一旦他们选择了上述之一,他们就会输入预约日期,如果他们选择“新”并输入约会的描述,最后他们会选择约会(1 =一次,2 =每日,3 =每月)。

我收到“ if(list.i.compareTo(lowDate)&lt; = 0)&amp;&amp;(list.i.compareTo(highDate)&gt; = 0); < / strong>“它给了我错误” AppointmentNew.java:69:非法开始表达“。我不确定为什么它会给我这个错误!

这是我的代码,我相信我已完成所有工作以完成上述任务!

import java.util.*;

public class AppointmentNew 
{
public static void main (String[] args)
{
  List<String> list = new ArrayList<String>();
  Scanner stdin = new Scanner(System.in);
  String choice = "";
  int choiceNum = 0;
  String date = "";
  String descrip = "";
  int type = 0;
  String typeChose = "";

  System.out.println("Welcome to Appointment App!\n");
  System.out.println("\t============================\n");

  do
  {
     System.out.print("\tMake Choice ( 1: New, 2: Print Range, 3: Print All, quit): ");
     choice = stdin.nextLine();

     choiceNum = Integer.parseInt(choice);

     if (choiceNum == 1)
     {
        System.out.print("\n\n\tEnter New Appointment Date in mm/dd/yyyy format: ");
        date = stdin.nextLine();

        System.out.print("\n\n\tEnter New Appointment Description: ");
        descrip = stdin.nextLine();

        System.out.print("\n\n\tEnter Type (1 = Once, 2 = Daily, 3 = Monthly): ");
        type = stdin.nextInt();
        if (type == 1)
        {
          Once once = new Once(date, descrip);
           typeChose = "One-Time";
        }
        else if (type == 2)
        {
          Daily daily = new Daily(date, decrip);
           typeChose = "Daily";
        }
        else
        {
          Monthly monthly = new Monthly(date, descrip);
           typeChose = "Monthly";
        }
          String stringToAdd = "";
          strinToAdd = "New " + descrip + " Appointment Added for " + date;
          list.append(stringToAdd);

        System.out.println("\n\n\tNew " + typeChose + " Appointment Added for " + date);

     }

     if (choiceNum == 2)
     {
     System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
     String lowDate = stdin.nextLine();
     System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");
     String highDate = stdin.nextLine();

     for(int i = 0; i < list.size(); i++)
        {
         int dateSpot = list.i.indexOf(" ");
           if (list.i.compareTo(lowDate) <= 0) && (list.i.compareTo(highDate) >= 0);
        {
           System.out.println(list.i);   
       }}
     }

     if (choiceNum == 3)
     {
       for(int i = 0; i < list.size(); i++)
       {
          System.out.println(list.i);     
       }
     }

  }while (choice != "quit");      

} }

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

用于访问列表元素的语法list.i不正确。您应该使用get(int)方法,例如list.get(i),获取列表中的元素。

此外,整个if条件必须用自己的括号括起来,并且if和它下面的大括号中的块之间不应该有分号:

if ((list.get(i).compareTo(lowDate) <= 0) && (list.get(i).compareTo(highDate) >= 0))
{
    System.out.println(list.get(i));
}

还有许多其他错误,包括尝试将String值与==进行比较(不要这样做,使用String的equals方法),以及括号关闭问题。

答案 1 :(得分:0)

有很多问题。要访问Collection中的列表项,您需要使用get(int)方法...

list.get(i)

if语句形成错误......

if (list.i.compareTo(lowDate) <= 0) && (list.i.compareTo(highDate) >= 0);
                                  ^-- Bad bracket
                                                                        ^-- This shouldn't be here

应该是

if (list.get(i).compareTo(lowDate) <= 0 && list.get(i).compareTo(highDate) >= 0)

注意,你有一个额外的结束括号,你不需要它,语句末尾的;将使语句变得多余(即它实际上不会做任何事情)