import java.util.*;
public class TypeTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter an int: ");
try {
int int_val = scan.nextInt();
System.out.println(int_val);
System.out.println("Using the int value by doubling it: " + int_val * 2); //I can multiply the int value by an int, I cannot multiply a String or it would throw a type conflict
}
catch (Exception e) {
System.out.println("Not an int, int is in the form: 194");
}
System.out.println("Enter a double: ");
try {
double double_val = scan.nextDouble();
System.out.println(double_val);
System.out.println("Using the double value by doubling it: " + double_val * 2.0); //I can multiply the double value by both a double or an int, I cannot multiply a String or it would throw a type conflict
}
catch (Exception e) {
System.out.println("Not a double, double is in the form: 1.94");
}
System.out.println("Enter a boolean: ");
try {
boolean bool_val = scan.nextBoolean();
System.out.println(bool_val);
if (bool_val) //
{
System.out.println("It worked");
}
else
{
System.out.println("It didn't work");
}
}
catch (Exception e) {
System.out.println("Not a boolean, boolean is in the form: true/false");
}
while (true)
{
System.out.println("Enter a char: ");
String char_val = scan.next();
if (char_val.length() == 1)
{
System.out.println(char_val);
break;
}
else
{
System.out.println("Not a char, char is in the form: a (only one character long)");
}
}
System.out.println("Enter a String: ");
try {
String str_val = scan.next();
System.out.println(str_val);
System.out.println("Output the last character of the String: " + str_val.charAt(str_val.length() - 1));//String is an array of characters so you can get the value of an element in the String
}
catch (Exception e)
{
System.out.println("How did you mess this up?");
}
scan.close();
}
}
TypeTest.main(null);
Usage of Primitives
Applying primitive data types
import java.util.*;
public class Operations {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the temperature as a whole number in Fahrenheit");
try {
int temp1 = scan.nextInt();
temp1 -= 32;
int celsiuscon = (temp1-32) * 5/9;
System.out.println(celsiuscon);
}
catch (Exception e)
{
System.out.println("Enter it as a whole number, no decimals");
}
System.out.println("Now enter the temperature as a decimal in Fahrenheit");
try {
Double temp1 = scan.nextDouble();
temp1 -= 32;
Double celsiuscon = temp1 * 5/9;
System.out.println(celsiuscon);
}
catch (Exception e)
{
System.out.println("Enter it as a decimal");
}
}
}
Operations.main(null);
Enter the temperature as a whole number in Fahrenheit
50
Now enter the temperature as a decimal in Fahrenheit
50.55555555555556