Question

This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input...

This is in Java.

Write the application, TheSentinel  to read a set of integers. Stop reading input when a non-integer is read. Use this exact prompt: System.out.print("Enter an integer or Q to quit: ");

Note: you will actually quit on any non-integer.

Do the following things:

  • Find and print the sum of all the even numbers
  • Find and print the smallest of the inputs
  • Determine if the number 7 is in the input. If 7 is in the inputs, print "7 is my lucky number" otherwise print "no sevens"
  • Print the number of values that are negative (less than 0)

You will only read the inputs one time and do all the processing as you go. No arrays yet.

The outputs should be on separate lines and in the order given above. If there were no inputs, just print "no input"

Must past the codecheck: http://www.codecheck.it/files/19010102198enlg7o8pozf8s2hl7ertifhd

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

I have to modify you program and made as requirment. Let me know if any chnage required,.

import java.util.Scanner;

/**
* Get a set of integers and print some infor about them
*/
public class DecisionsWithInput
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int counter = 0;
int input = 0;
int sum = 0;
int min = 0;
int positive = 0;
boolean first = true, isFive = false;
System.out.print("Enter an integer or Q to quit: ");

while(!scan.hasNext("Q"))
{
if (scan.hasNext("Q") || scan.hasNext("x") || scan.hasNext("1.5")) {
break;
}
System.out.print("Enter an integer or Q to quit: ");
// input = Integer.parseInt(inString);
try {
input = Integer.parseInt(scan.next());
if(first)
{
min = input;
first = false;
}
if(min > input)
{
min = input;
}
if(input % 2 == 1 ||input % 2 == -1)
{
sum += input;
}
if(input > 0)
{
positive++;
}
if(input == 5)
{
isFive = true;
}
counter++;
} catch (NumberFormatException e) {
//System.out.println("\nEnter only Integer");
break;
}
}

if (counter==0) {
System.out.println("No Input");
}else {
System.out.println(sum);
System.out.println(min);
if(isFive)
{
System.out.println("5 is my lucky number");
}
else
{
System.out.println("no fives");
}
System.out.println(positive);
}
}
}

Add a comment
Know the answer?
Add Answer to:
This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • Problem 4: Write a Java program that reads positive integers, find the largest of them, count...

    Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out the average of all input integers up to two decimal places (小數 點第二位). Use the characters·Q' or'q, to end the input. Below are two sample runs: Enter an integer, or quit with Q or q:1 Enter an integer, or quit with Q or q: 2 Enter an integer, or quit with Q or q: 3 Enter an integer, or...

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • IN JAVA Write a program that first gets a list of integers from input. The input...

    IN JAVA Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75...

  • in java Write an application called Squaring that will: • Prompt the user to enter an...

    in java Write an application called Squaring that will: • Prompt the user to enter an integer greater than 1. Your code for this should be type-safe and validate the number, meaning that your code should re-prompt the user for another entry if they enter anything other than an integer that is greater than 1. • Repeatedly square the integer until it exceeds 1 million, outputting the value each time after squaring. • Output the number of squarings required to...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT