1

Write a program that uses a while loop. In each iteration of the loop, prompt the user to enter a number – positive, negative, or zero. Keep a running total of the numbers the user enters and also keep a count of the number of entries the user makes. The program should stop whenever the user enters “q” to quit. When the user has finished, print the grand total and the number of entries the user typed.

I can get this program to work when I enter a number like 0, to terminate the loop. But I have no idea how to get it so that a string stops it.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    int num;
    System.out.println("Enter an integer, enter q to quit.");
    num = in.nextInt();
    while (num != 0) {
        if (num > 0){
        sum += num;
        }
        if (num < 0){
        sum += num;  
        }
        count++;
        System.out.println("Enter an integer, enter q to quit.");
        num = in.nextInt();
    }
    System.out.println("You entered " + count + " terms, and the sum is " + sum + ".");

}
1
  • You mean like trying to read in a string and checking to see if it's equal to "q"? (plus checking to see if the user inputted a number, and so on) Commented Feb 22, 2014 at 23:20

3 Answers 3

1

Your strategy would be to get the input as a string, check to see if it is a "q", and if not convert to number and loop.

(Since this is your project, I am only offering strategy rather than code)

This is the rough strategy:

String line;
line = [use your input method to get a line]
while (!line.trim().equalsIgnoreCase("q")) {
   int value = Integer.parseInt(line);
   [do your work]
   line = [use your input method to get a line]
}
Sign up to request clarification or add additional context in comments.

1 Comment

We can suggest some methods for you to use as well if you're not comfortable or don't know how to check up with the Java API
1
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    String num;
    System.out.println("Enter an integer, enter q to quit.");
    num = in.next();
    while (!num.equals("q")) {
          sum += Integer.parseInt(num);
        count++;
      System.out.println("Enter an integer, enter q to quit.");
      num = in.next();
    }
    System.out.println("You entered " + count + " terms, and the sum is " + sum + ".");

  }

Cuts down on your code abit and is simple to understand and gives you exactly what you want.

could also add an if statement to check if they entered another random values(so program doesn't crash if the user didn't listen). Something like:

if(isLetter(num.charAt(0))
  System.out.println("Not an int, try again");

Would put it right after the while loop, therefore it would already of checked if it was q.

Comments

0

java expects an integer but we should give the same exception. One way to solve this problem is entering a String, so that if the user first pressing is the Q, never enters the cycle, if not the Q. We assume that the user is an expert and will only enter numbers and the Q when you are finished. Within the while we convert the String to number with num.parseInt (String)

Integer num;
String input;
while(!input.equal(q)){
 num=num.parseInt(input)
  if(num<0)
   sum+=1;
  else
   sumA+=1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.