Week 6 - Level 1 - 10 (Step) Rithish S
Week 6 - Level 1 - 10 (Step) Rithish S
Regno:RA2411003012057
Sec:L2
1. Write a program to compare two strings using the charAt() method and check the result with the
built-in String equals() method.
import java.util.Scanner;
if (charAtResult == equalsResult) {
System.out.println("✅ Both methods give the same result.");
} else {
System.out.println("❌ Methods give different results.");
}
scanner.close();
}
}
Output:
Enter first string: code
Enter second string: code
Result from charAt() comparison: true
Result from equals() method: true
✅ Both methods give the same result.
2. Write a program to create a substring from a String using the charAt() method. Also, use the String
built-in method substring() to find the substring of the text. Finally Compare the the two strings
and display the results.
import java.util.Scanner;
scanner.close();
}
}
Output:
Enter the main string: programming
Enter start index: 3
Enter end index: 8
3. Write a program to return all the characters in a string using the user-defined
method, compare the result with the String built-in toCharArray() method, and dis-
play the result.
import java.util.Scanner;
scanner.close();
}
}
Output:
Enter a string: java
Characters from user-defined method: j a v a
Characters from toCharArray() method: j a v a
Are both arrays equal? True
handleException();
}
}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()"
because "text" is null
at NullPointerDemo.generateException(NullPointerDemo.java:5)
at NullPointerDemo.main(NullPointerDemo.java:15)
import java.util.Scanner;
handleException(input);
scanner.close();
}
}
Output:
Enter a string: java
Caught StringIndexOutOfBoundsException: String index out of range: 10.
import java.util.Scanner;
handleException(input);
scanner.close();
}
}
Output:
Enter a string: example
Caught IllegalArgumentException: begin 5, end 2, length 7.
import java.util.Scanner;
public class NumberFormatDemo {
handleException(input);
scanner.close();
}
}
Output:
Enter a number as text: abc
Caught NumberFormatException: For input string: "abc"
import java.util.Scanner;
Output:
Enter text: Hello World 123
10. Write a program to convert the complete text to lowercase and compare the results.
import java.util.Scanner;
scanner.close();
}
}
Output:
Enter text: HeLLo WorLD 123