0

I am writing a program in Java that allows the user to select a choice (1 : New, 2: Print Range, 3: Print All, quit). Once they choose one of the above they then enter the appointment date if they chose "New" and also enter a description for the appointment and lastly they pick if the appointment will be (1 = Once, 2 = Daily, 3 = Monthly).

I am getting an error on the line "if (list.i.compareTo(lowDate) <= 0) && (list.i.compareTo(highDate) >= 0);" It is giving me the error "AppointmentNew.java:69: illegal start of expression". I am not sure exactly why it is giving me that error!

Here is my code, I believe I have it all completed to carry out the above tasks!

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

} }

Thank you in advance for any help!

4

2 回答 2

1

The syntax list.i for accessing an element of the list is incorrect. You should use the get(int) method, e.g. list.get(i), to get an element of the list.

Additionally, the entire if condition must be enclosed its own parentheses, and there shouldn't be a semicolon following between the if and the block in braces below it:

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

There are numerous other errors, including attempting to compare a String value with == (don't do that, use String's equals method), plus brace closing issues.

于 2013-04-19T00:33:31.077 回答
0

There are a number of problems. To access a list item in a Collection you would need to use the get(int) method...

list.get(i)

The if statement is formed wrong...

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

Should be

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

Note, you have an extra closing bracket where you don't need it and the ; at the end of the statement is going to make the statement redundant (ie it won't actually do anything)

于 2013-04-19T00:40:13.070 回答