user3342038
user3342038

Reputation: 63

Terminating loops with strings. (Java)

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 + ".");

}

Upvotes: 1

Views: 3481

Answers (3)

Lain
Lain

Reputation: 2216

  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.

Upvotes: 1

ErstwhileIII
ErstwhileIII

Reputation: 4843

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]
}

Upvotes: 1

santiago92
santiago92

Reputation: 297

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;
}

Upvotes: 0

Related Questions