Primitives Notebook
import java.util.Scanner;
public class ScanPrimitives {
public static void main(String[] args) {
Scanner input;
// enter number
input = new Scanner(System.in);
System.out.print("Enter an integer: ");
try {
int sampleInputInt = input.nextInt();
System.out.println(sampleInputInt);
} catch (Exception e) {
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
//enter a number that can contain decimals
input = new Scanner(System.in);
System.out.print("Enter a double: ");
try {
double sampleInputDouble = input.nextDouble();
System.out.println(sampleInputDouble);
} catch (Exception e) {
System.out.println("Not an double (form like 9.99), " + e);
}
input.close();
// enter a true or false
input = new Scanner(System.in);
System.out.print("Enter a boolean: ");
boolean firstInputBoolean = input.nextBoolean();
System.out.println(firstInputBoolean);
System.out.print("Enter 2nd boolean: ");
boolean secondInputBoolean = input.nextBoolean();
System.out.println(secondInputBoolean);
try {
if (firstInputBoolean != secondInputBoolean) { // one input was true and one was false
System.out.print("The Or gate is true, as the inputted booleans are different.");
}
else { // the inputs were the same
System.out.print("The Or gate is false, as the inputted booleans are the same.");
}
} catch (Exception e) {
System.out.println("Not an boolean (needs to be true or false), " + e);
}
input.close();
System.out.println("");
// enter a string (basically anything)
input = new Scanner(System.in);
System.out.print("Enter a String: ");
try {
String sampleInputString = input.nextLine();
System.out.println(sampleInputString);
} catch (Exception e) {
System.out.println("Not an String, " + e);
}
input.close();
}
}
ScanPrimitives.main(null);
int divided = 5 / 2;
double doubleDivided = 5.0 / 2;
System.out.println("When the integer was divided, instead of 2.5 like the double, the answer turns out to be 2, so it seems like the calculation was truncated.");
System.out.println("Integer 5 / 2 is " + divided);
System.out.println("Double 5 / 2 is " + doubleDivided);
import java.util.Scanner;
public class main {
public static void main(String[] args){
Scanner input;
input = new Scanner(System.in);
System.out.print("Enter 1st string: ");
String firstInputString = input.nextLine();
System.out.println(firstInputString);
System.out.print("Enter 2nd string: ");
String secondInputString = input.nextLine();
System.out.println(secondInputString);
try {
firstInputString += secondInputString;
System.out.println("The strings combined are: " + firstInputString + ".");
}
catch (Exception e) {
System.out.println("Enter a string" + e);
}
}
}
main.main(null);
import java.util.Scanner;
public class main {
public static void main(String[] args){
Scanner input;
input = new Scanner(System.in);
System.out.println("Enter a number of pounds: ");
try {
double inputPounds = input.nextDouble();
var outputPounds = inputPounds;
outputPounds = outputPounds * 0.453592;
System.out.println(inputPounds + " pounds is " + outputPounds + " kg.");
}
catch (Exception e) {
System.out.println("Enter a number" + e);
}
}
}
main.main(null);