Question: trying to write code underneath the appropriate comment. Needs to ask user to enter in a positive integer 5 times. Need to print out each
trying to write code underneath the appropriate comment. Needs to ask user to enter in a positive integer 5 times. Need to print out each integer after its entered. Using a call to method inputInteger. Here is the code so far
package lab2Package;
import java.util.Scanner;
public class Lab2Class
{
public static void main(String[] args)
{
//declare variables
String name;
int userNum;
//initialize variables
name = inputString("Enter your name\t");
//enter your name at the keyboard
System.out.println("My name is "+ name);
//enter 5 integers at the keyboard and print it out
System.out.println("Integers from Keyboard");
//enter 5 decimal numbers at the keyboard and print each one out
System.out.println("Decimal values from keyboard");
//generate 20 random numbers and print each one out
System.out.println("Random numbers between one and two inclusively");
}//end main
//
// inputString
//
// the purpose of this method is to input a string from the keyboard
//
// Input: prompt the prompt message for the dialog box
//
// Return: inputStr the string that was entered at the keyboard
//
public static String inputString(String prompt)
{
Scanner input = new Scanner(System.in);
String inputStr;
//prompt user to enter in string
System.out.print(prompt);
//read in value as string from keyboard
inputStr = input.nextLine();
//return input string
return(inputStr);
}//end input string
//
// inputInteger
//
// the purpose of this method is to input an integer value form the keyboard
//
// Input: prompt the prompt message for the user
//
// Return: num the integer entered at the keyboard
//
public static int inputInteger(String prompt)
{
Scanner input = new Scanner(System.in);
String cleanUpStr;
int num;
num = 0;
cleanUpStr = "no string yet";
System.out.println(prompt);
num = input.nextInt();
cleanUpStr = input.nextLine();
return(num);
}//end inputInteger
}//end lab2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
