This project deals with EOF-based loops, using the while construct where we do not know the exact number of times a loop will iterate (perform the associated block of code). We will continue to build on our knowledge of if/else constructs.

Use the variables given below to solve the problem. You should need no other variables to complete this project.

// number entered and the running total
int inputNum, sum = 0;

// count of valid and invalid numbers seen
int numValid = 0, numInvalid = 0;
double average;
Using your knowledge of Java and using inputNum as your input variable, write a while loop to read positive integers from the user until the user enters the EOF character. This is the sentinel value indicating that the user has finished entering data.

You can use hasNext() to determine if the EOF has occurred. The user must use CTRL-D (DrJava) to terminate the program since these are the appropriate EOF characters.

Numbers will be separated into two categories. Valid numbers are considered to be the range of positive integers including zero. Invalid numbers are the range of negative integers.

As you are reading in the values you should be keeping track of how many positive values (numValid) have been read while simultaneously keeping a running total (sum).

You should not include negative values in the running total. Instead you should increment the count of invalid numbers (numInvalid) entered by the user.

You will report to the user the number of valid values they entered, the number of invalid values, the sum and average of the valid values. The average should be displayed as a floating point value to two decimal places using the printf() method.

A sample run should look something like the following:

Enter a positive value (EOF to quit): 4
Enter a positive value (EOF to quit): 7
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 2
Enter a positive value (EOF to quit): -1
The number "-1" is invalid.

Enter a positive value (EOF to quit): 8

import java.util.*; 

public class p5

{ 
   static Scanner kb = new Scanner(System.in); 
   public static void main(String[] args) 
 { 
    String s;
    // number entered and the running total
int inputNum, sum = 0, x ;

// count of valid and invalid numbers seen
int numValid, numInvalid;
double average;

    System.out.print("Enter a positive value (CTRL-D to end): ");
    while ( kb.hasNext() ) 
    {

     s = kb.nextLine();

     System.out.print("Enter a positive value (CTRL-D to end): ");



    }
 }

  public static int numValid(String s) {

    int count=0;
    for (int x = 0; x < s.length(); x++){
      char c = s.charAt(x) ;

      //System.out.println(s.charAt(x));
      if ( Character.isDigit(c) )
        count ++;
    }
  return count;}
}

Enter a positive value (EOF to quit): -4
The number "-4" is invalid.

Enter a positive value (EOF to quit): CTRL-D

There were 6 valid numbers entered.
The sum of the valid numbers was 29 and the average was 4.83.
There were 2 invalid numbers.

Recommended Answers

All 10 Replies

while ( kb.hasNext() ) {
     s = kb.nextLine();

     /* end the loop if EOF */
     if(s==null || s.equals(""))
         break;

     /* check if the input is valid or not */
     int newNum=numValid(s);

     if(newNum<0) {
         System.out.println("The number \"-1\" is invalid.")
     } else {
         /* if the number is valid, add it to sum */
         sum+=newNum;
     }

     System.out.print("Enter a positive value (CTRL-D to end): ");
 }

rewrite the function numValid(String) such that it returns -1 if the number is less than 0 and the count, if it is positive

does this work i keep getting one error about the numValid

numValid is a function you need to create..look at your original source code

it should return -1 if the number is less than 0 and return the count if it is positive

public static int numValid(String s) {
    //your code here
}
import java.util.*; 

public class p5

{ 
   static Scanner kb = new Scanner(System.in); 
   public static void main(String[] args) 
 { 
    String s;
    // number entered and the running total
int inputNum, sum = 0, x ;

// count of valid and invalid numbers seen
int numValid, numInvalid;
double average;

    System.out.print("Enter a positive value (CTRL-D to end): ");
    while ( kb.hasNext() ) 
    {

     s = kb.nextLine();
     /* end the loop if EOF */
    }
   }
    public static int numValid(String s){
      int sum =0; 

     if(s==null || s.equals(""))
         break;
     /* check if the input is valid or not */
     int newNum = numValid(s);
     if(newNum<0) {
         System.out.println("The number \"-1\" is invalid.");
     } else {
         /* if the number is valid, add it to sum */
         sum+=newNum;
     }
     System.out.print("Enter a positive value (CTRL-D to end): ");
}
   }

i keep getting an error

"i keep getting an error" tells us nothing. If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be.

import java.util.*; 
public class p5 { 
   static Scanner kb = new Scanner(System.in); 

   public static void main(String[] args) { //main function starts here

         /* write your code here for main function */

    } //main function ends here


    public static int numValid(String s) { //numValid function starts here

       /* write your code here for numValid function */

    } //numValid function ends here
}

you should write functions like this..one function declaration should be outside of another function declaration

see how main function ends before start of numValid function

1 error found:
File: /Volumes/JOJO/computer 2014/p5.java [line: 29]
Error: /Volumes/JOJO/computer 2014/p5.java:29: break outside switch or loop
thats the error

i wrote the code like that and the break is the error on line 29

Well yes, like it says, you can only use break insode a switch or loop. What did you intend to do with your break?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.