So I am using the scanner utility for user inputs. There are 3 parts. Integer arithmetic, Real arithmetic and String operations. I am getting desired results with first 2 but I am stuck at the last one.
Issue-
String c1 = keyboard.nextLine(); is not prompting for user input. If I use .next instead of .nextLine it prompts but the rest of the string operations are coming out wrong as in the length will only display the length of first word and the not the whole sentence.
Any help will be appreciated.
import java.util.Scanner; public class Lab2 { /** * @author */ public static void main(String[] args) { int a1,a2,a3; System.out.println("*** Test integer arithmetic ***\n"); Scanner keyboard = new Scanner(System.in); System.out.print("Enter first integer number: "); a1 = keyboard.nextInt(); System.out.print("Enter second integer number: "); a2 = keyboard.nextInt(); a3 = a1 + a2; System.out.print(a1 + " + " ); System.out.print(a2 + " = " ); System.out.println(a3); a3 = a1 - a2; System.out.print(a1 + " - " ); System.out.print(a2 + " = " ); System.out.println(a3); a3 = a1 * a2; System.out.print(a1 + " * " ); System.out.print(a2 + " = " ); System.out.println(a3); a3 = a1 / a2; System.out.print(a1 + " / " ); System.out.print(a2 + " = " ); System.out.println(a3); a3 = a1 % a2; System.out.print(a1 + " % " ); System.out.print(a2 + " = " ); System.out.println(a3); double b1,b2,b3; System.out.println("*** Test real arithmetic ***\n"); System.out.print("Enter first integer number: "); b1 = keyboard.nextDouble(); System.out.print("Enter second integer number: "); b2 = keyboard.nextDouble(); b3 = b1 + b2; System.out.print(b1 + " + " ); System.out.print(b2 + " = " ); System.out.println(b3); b3 = b1 - b2; System.out.print(b1 + " - " ); System.out.print(b2 + " = " ); System.out.println(b3); b3 = b1 * b2; System.out.print(b1 + " * " ); System.out.print(b2 + " = " ); System.out.println(b3); b3 = b1 / b2; System.out.print(b1 + " / " ); System.out.print(b2 + " = " ); System.out.println(b3); b3 = b1 % b2; System.out.print(b1 + " % " ); System.out.print(b2 + " = " ); System.out.println(b3); System.out.println("*** Test String operations ***\n"); System.out.print("Enter a string of characters: "); String c1 = keyboard.nextLine(); int len = c1.length(); System.out.println("The length of string \"" + c1 + "\" is " + len); System.out.print("Enter an integer between 0 and 20: "); int cha = keyboard.nextInt(); char ch = c1.charAt(cha); System.out.println("The character at index \"" + cha + "\" of string \"" + c1 + "\" is " + ch); System.out.print("Enter another string of characters: "); String c2 = keyboard.next(); int indx = c1.indexOf(c2); System.out.println("The first occurrence of string \"" + c2 + "\" in string \"" + c1 + "\" is at position " + indx ); } }