3
 try {


    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the continent;");
    String CN = in.readLine();
    String MaxDate="1";
    for(Earthquakerecd e : eqList)
    {
        if( e.getContinent().equals("CN"))
        {
          MaxDate=e.getDate();
        }

        {
            System.out.println( e.toString()); 
        }


    }

     System.out.println( MaxDate); 
    }

    catch (IOException e)
    {
        System.out.println("IOException has been caught");
    }

This is a simple problem i think. In this problem Maxdate is declared as 1. CN is a string for continent . If the user input matches the continent , then the date should be passed from e.getDate() to Maxdate. In anycase , we should never get the output as 1 , it should be some date from object e. I am always getting 1 for Maxdate. Any possible solutions? is my syntax right?

5
  • have you tried MaxDate=e.getDate().toString()? I'm not savvy with Java, but worths a shot Commented Feb 7, 2012 at 23:05
  • the date attribute in object e is a String by the way Commented Feb 7, 2012 at 23:06
  • Why is MaxDate a String? Make it same class e.getDate() returns... Also btw, in Java convention is to start variables with lowercase, and classes with uppercase. Commented Feb 7, 2012 at 23:06
  • Are you sure you're getting inside the if? Put a breakpoint there. Commented Feb 7, 2012 at 23:08
  • @da_scav_from_windy_city You should accept an answer here. Both answers are correct and dbyrne's is very good. Commented Feb 25, 2012 at 1:23

2 Answers 2

7

Seems like you want:

if(e.getContinent().equals(CN))

Right now you are comparing against the String literal "CN". You aren't using the variable CN for anything.

I'm assuming you meant to insert an else after your if statement?

Also, in Java it's common to not start your variable names with uppercase letters (name your string cn instead of CN).

Sign up to request clarification or add additional context in comments.

Comments

1

Try doing it this way:

try {


    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the continent;");
    String CN = in.readLine();
    String MaxDate="1";
    for(Earthquakerecd e : eqList)
    {
        if( e.getContinent().equals(CN)) //When you put it with "" the continent value was compared to CN and not from the user input
        {
          MaxDate=e.getDate();
        }

        {
            System.out.println( e.toString()); 
        }


    }

     System.out.println( MaxDate); 
    }

    catch (IOException e)
    {
        System.out.println("IOException has been caught");
    }

if that doesnt work, then try by doing the for on this way:

for (int i=0; i<eqList.size(); i++) {
     Earthquakerecd e = eqList.get(i);
     //... code ... //
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.