Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing value from object string to string

Tags:

java

arrays

 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?

like image 865
Nidhin_toms Avatar asked May 13 '26 04:05

Nidhin_toms


2 Answers

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).

like image 192
dbyrne Avatar answered May 15 '26 16:05

dbyrne


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 ... //
}
like image 31
user1084509 Avatar answered May 15 '26 16:05

user1084509



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!