Part III. Common String Errors
The following two example programs demonstrate common errors that often occur when programming with Strings.
public class StringErrors1 { public static void main(String [] args) { String greeting = "hello world"; greeting.toUpperCase(); System.out.println(greeting); } } |
What does this program do?
The program creates a String ‘hello world’ and store in a variable, convert to upper case (but not store the resultant String) and display the String. As a result, the program prints ‘hello world’ (as in original text). But storing the result in a variable (or the same variable itself), the program displays ‘HELLO WORLD’.
How might you make the program more user-friendly?
By asking user to enter a text and convert to upper case and display the resultant text, preferably in a GUI.
Updated program:
import javax.swing.JOptionPane;
public class StringErrors1 {
public static void main(String[] args) {
// prompting and reading a text from user
String greeting = JOptionPane.showInputDialog("Enter some text: ");
// converting to upper case and storing the resultant text in greeting
greeting = greeting.toUpperCase();
// displaying the converted text
JOptionPane.showMessageDialog(null, greeting);
}
}
If we enter “a b c”, the program will display “A B C”, if we do leave the input empty and press enter, no text will be displayed as the input is an empty String. (Same thing happens if we use nextLine() method in Scanner class or readLine() method in BufferedReader class), but if we press cancel button on JOptionPane window or closed the window, an Exception will be thrown.
The thrown exception will be NullPointerException.
The line ‘greeting=greeting.toUpperCase();’ is the one throwing the exception.
This is because when we click cancel or close the JoptionPane window, it returns null value, so that null value will be stored in greeting. If the greeting variable is null, we cannot call toUpperCase() method in it. If we do, it will cause NullPointerException.
Part III. Common String Errors The following two example programs demonstrate common errors that often occur...
Analyze the following code: public class Test { public int x; public Test(String t) { System.out.println("Test"); public static void main(String[] args) { Test test: System.out.println(test.x); The program has a compile error because Test class does not have a default constructor The program has a compile error because test is not initialized OO The program has a compile error because x has not been initialized The program has a runtime NullPointerException while executing test.x because test is a null reference and...
Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...
Unreachable statement???????
CSIT 111 home> 313: More string operations E zyBooks catalog Print 'Censored if userinput contains the word 'darn, else print userinput. End with newline Note: These activities may test code with different test values. This activity will perform three tests, with userinput of That darn cat', then with Dang, that was scary!", then with I'm darning your socks.. See How to Use zyBooks . Also note: If the submitted code has an out-of-range access, the system will stop...
The files provided contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. import java.util.*; public class DebugEight1 { public static void main(String args[]) { Scanner input = new Scanner(System.in); char userCode; String entry, message; boolean found = false; char[] okayCodes = {'A''C''T''H'}; StringBuffer prompt = new StringBuffer("Enter shipping code for this delivery\nValid codes are: "); for(int x = 0; x <...
Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates the error handling routine from the rest of the code. In this application, you practice handling exception errors. You use sample code that was purposefully designed to generate an exception error, and then you modify the code so that it handles the errors more gracefully. For this Assignment, submit the following program: The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import...
Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...
QUESTION 3 Assume x = 4 and y = 5, which of the following is true? x < 5 || y < 5 x > 5 || y > 5 x < 5 && y < 5 x > 5 && y > 5 QUESTION 14 Which of the following statements are the same? (A) x -= x + 4 (B) x = x + 4 - x (C) x = x - (x + 4) (A) and (B) are...
The following individual two programs are using while loop in order to print a specific output: The outputs of the first program are: 10 9 8 5 4 The outputs of the second program are: 123456789 Using the Table 3.1 below, allocate the error(s) of each line of two programs and type of error: The first program: public Main { public static void main(String args[]){ int i=10 while(i>4){ System.out.print(i); i++; } } } The second program: public class Main {...
The following individual two programs are using while loop in order to print a specific output: The outputs of the first program are: 10 9 8 5 4 The outputs of the second program are: 123456789 Using the Table 3.1 below, allocate the error(s) of each line of two programs and type of error: The first program: public Main { public static void main(String args[]){ int i=10 while(i>4){ System.out.print(i); i++; } } } The second program: public class Main {...
use python IDEL
Please highlight the answer
1 ווCIוסטIT Errors and Exceptions. There are two main types of errors: syntax errors and runtime errors. Syntax errors are detected by the Python interpreter before the program execution during the parsing stage (parsing is a process, during which the Python interpreter analyzes the grammatical structure of the program). Runtime errors are errors in a program that are not detected by the Python interpreter during its parsing stage. They are further divided into...