0

I am trying to make both of these if statements output if true.

Like if I am enter:

cat
car

I want it to output:

Cat and Car are both the same length. 
Cat and Car both start with C.

Right now I am only getting the First output

Here is the code:

import java.util.Scanner;  // Import the Scanner class


public class Main
{
    public static void main(String[] args) {
        System.out.println("input words");

        String myObj, myObj1;

         Scanner sc = new Scanner(System.in);  // Create a Scanner object
         myObj = sc.nextLine();  // String Input
         myObj1 = sc.nextLine(); // String Input


         if(myObj.length() == myObj1.length()){  // System check for String Length
             System.out.println( myObj + " and " + myObj1 + " are the 
            same length.");

         }
         if ((myObj1.charAt(0) == 'C') && (myObj.charAt(0) == 'C')){

            System.out.println(myObj + " and " + myObj1 + " start with C." );
            } // Output if both start with C
5
  • After myObj = sc.nextLine(); // String Input myObj1 = sc.nextLine(); // String Input please print out the values Commented Sep 27, 2019 at 1:23
  • Unable to reproduce. I get both. --- So does IDEONE. Commented Sep 27, 2019 at 1:24
  • works for me input words Car Cat Car and Cat are the same length. Car and Cat start with C. Commented Sep 27, 2019 at 1:25
  • 1
    BTW c does not equal C Commented Sep 27, 2019 at 1:25
  • @ScaryWombat Oh right, if OP enters cat, not Cat, then the second output will not show. Nicely spotted, Mr. Wombat. Commented Sep 27, 2019 at 1:27

1 Answer 1

1

if ((myObj1.charAt(0) == 'C') && (myObj.charAt(0) == 'C')){

will not be true for the input car ot cat as

c does not equal C

After reading the values add this code

myObj = myObj.toUpperCase();
myObj1 = myObj1.toUpperCase();
Sign up to request clarification or add additional context in comments.

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.