/** * Author: Josiah Yoder * Class: SE1011-011 * Lesson: Week 5, Day 1 */ import java.text.ParseException; import java.util.Scanner; import javax.swing.JOptionPane; import java.util.Random; public class Example011_5_1 { public static void main(String[] ignored) { Scanner in = new Scanner(System.in); // Parse the first digit with Integer.parseInt() System.out.println("Enter a number:"); String str = in.next(); int number = 0; if(str.length() > 0) { char c = str.charAt(0); String str2 = str.substring(0, 1); // alternative: String str2 = Character.toString(str.charAt(0)); boolean isDigit = Character.isDigit(c); //not char here. if(isDigit) { number = Integer.parseInt(str2); } } // Parse the first digit with Character.digit(c,10); System.out.println("Enter a number:"); str = in.next(); number = 0; if(str.length() > 0) { char c = str.charAt(0); boolean isDigit = Character.isDigit(c); //not char here. if(isDigit) { int digit = Character.digit(c,10); } } System.out.println("The first digit is "+number); // Use Math.pow to raise 5 to the power of 4 // double a = 5; // double b = 4; // Math.pow(5,4); // Very simple pseudo-random number generator // int limit = 7; // int increment = 4; // int currentValue = 0; // for(int i = 0; i < 10; i++) { // currentValue+=increment; // currentValue = currentValue % limit; // System.out.println("The "+i+"th random number is "+currentValue); // } // Using Java's random number generator // Random generator = new Random(); // generator.setSeed(System.currentTimeMillis()); // Poor practice in Java, good practice in other languages. // int randomNumber; // for(int i = 0; i < 10; i++) { // randomNumber = generator.nextInt(5); // System.out.println("The "+i+"th random number is "+randomNumber); // } } }