JOptionPane, Floating-Point Values, Advanced Scanner Input, Global Constants, String Methods
JOptionPane There is a special library that imports a different type of input/output capability. It is called the JOptionPane. The following import should be on top of the program before using JOptionPane: import javax.swing.*; The asterisk, *, just means that whatever is in javax.swing is to be imported. To show a JOptionPane, the following statement can be placed in any method. JOptionPane.showMessageDialog(null, "Hello!"); Ignore the null for now, but always put it before the message. To have JOptionPane ask for input, a string, from the user, the following statement can be placed in any method. JOptionPane.showInputDialog(null, "Enter data:"); On run time, the JOptionPane will appear with "Enter data:" on the top and an input box below it. The input will always return a string data. In order for JOptionPane to return a numeric data, we use the parse method.
The first statement asks the user to enter a number. After that, it is held in a String variable called input. But since it is a number, we use the parse method to convert it into an integer that is stored inside an integer variable called num. The integer variable called result then adds three to this converted integer, which is then displayed in the JOptionPane for the user to see that their string data got converted into an integer. Working with Floating-Point Values (Double, Float, etc.) Never use the relational operator == when comparing doubles, floats, etc. This is because 0.1 is a stored double that actually equals 0.99999... instead. It is rounded when being used, not compared, to become 0.1. Instead, testing ranges is the most efficient way of comparing floating-point values. If you want to test if a floating-point value is 3.5, check to see if the number if sufficiently close to 3.5. final double MIN = 3.499999; final double MAX = 3.500001; if(dec < MAX && dec > MIN) { System.out.println("This number is x."); } else { System.out.println("This number is not x."); } This code will show that x is in between 3.499999 and 3.500001, which is very, very close to 3.5. Advanced Scanner Input If we want our scanner to determine what type of data is being put, we use the following statement after declaring the scanner object: keyboard.hasNextLine(); (is it a string?) keyboard.hasNextInt(); (is it a integer?) keyboard.hasNextDouble(); (is it a double?) keyboard.hasNextBoolean(); (is it a boolean?) To check if the input the user put in is a string, integer, double, etc., we use conditional statements. if(keyboard.hasNextInt()) { System.out.println("Your input is an integer."); } else if(keyboard.hasNextDouble()) { System.out.println("Your input is a double."); } else if(keyboard.hasNextBoolean()) { System.out.println("Your input is a boolean."); } else if(keyboard.hasNextLine()) { System.out.println("Your input is a string."); } else { System.out.println("Nothing."); } However, if a user does not put any input into the scanner (presses enter), the input will still appear until input is placed. So the else statement above is unnecessary. Integers should be checked first, because an integer can be a double. Also, boolean and everything else should go before checking if the input is a string, because all of them can be considered a string type. Global Constants If a constant must be seen by all methods, it should be declared as a global constant. Instead, regular constants are only seen inside the method they are declared in. For global constants, they are positioned at the top of the program, under the import statements. ... static final int PANTS_WEATHER = 15; public static void main... For a constant to be global, the keyword static is used in front of it. Variables can be global without being constant, however this is not good programming, because anyone can change the variable's value. String Methods There are many methods designed for numeric data, and there is also a separate many methods designed for string data. Equalizing Strings Instead of using the relational operator == to compare strings, which does not work, because every variable has a different memory location, we use the method .equals(). In fact, == only works for primitive data types, or the lowercase data types like int, double, boolean, etc. It even doesn't work properly for double, float, etc. if(string1.equals(string2)) { System.out.println("They are equal."); } The if statement will test if the string1 is the same as string2. If it's true, then the code will run. Comparing Strings If we want to know if strings come before other strings alphabetically, we use the .compareTo() method. String string1 = "abc"; String string2 = "def"; if(string1.compareTo(string2) < 0) { // string1 comes before string2 } You can read the if statement as string1 < string2. If this is true, which it is in this case, the code will run. Technically, .compareTo() does not compare if strings are in alphabetical order. If we were to compare, String string1 = "abc"; String string2 = "DEF"; logically string1 should go before string2. However, it doesn't. Instead, .compareTo() compares the DEC values in the ASCII table. Therefore, D has a lesser value than a, so, DEF comes before abc. To compare strings that have various case sensitivities, use the method .compareToIgnoreCase(). The reason .compareTo() exists is for comparing strings that are not case sensitive, making the program faster. It only has to look at string data, and not convert it into lowercase or uppercase. String Length The method .length() lets the user know how many characters, including white space, there is in total inside the string. String text = "hello world"; int len = text.length(); System.out.println(len); The output would equal 11, because there are 11 total characters, or ASCII characters inside the string variable text. Strings and Character Position In order to access a specific character in a string, each character has its own particular position inside the string. The .charAt() method tells the user where this character is positioned according to zero-based indexing. Zero-based indexing means the very first character, or element, is at position 0, or in the zeroth index. Therefore, if you were to determine the zeroth position in String str = "HELLO"; char ch = str.charAt(0); System.out.println(ch); will give us H.











