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
  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • 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...

  • Must use Array. Must be in JAVA. Write a program that uses a Scanner object to...

    Must use Array. Must be in JAVA. Write a program that uses a Scanner object to read in a sequence of integers. The first number in the sequence represents the number of integers in the sequence (i.e., do NOT include the first number as part of the sequence). Create an array of integers that is the same size as the number of integers. Then the program will read each integer and place it in the array. Print the content of...

  • 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...

  • Do pseudocode in java Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that...

    Do pseudocode in java Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and number of times it appeared as shown...

  • (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...

  • # Write PYTHON programs that read a sequence of integer inputs and print # a.  The...

    # Write PYTHON programs that read a sequence of integer inputs and print # a.  The smallest and largest of the inputs. # b.  The number of even and odd inputs. # c.  Cumulative totals. For example, if the input is 1 7 2 9, the program should print # 1 8 10 19. # d.  All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the # program should print...

  • 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...

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • 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...

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