Modify the RepeatedDigits program below so that the user can enter more than one
ID: 3632340 • Letter: M
Question
Modify the RepeatedDigits program below so that the user can enter more than one number to be tested for repeated digits. The program should terminate when the user enters a number that's less than or equal to 0. Please give single line comment and explanation next modified lines.
//////////////////////////////////////////////////////////////
// From JAVA PROGRAMMING: FROM THE BEGINNING, by K. N. King //
// Copyright (c) 2000 W. W. Norton & Company, Inc. //
// All rights reserved. //
// This program may be freely distributed for class use, //
// provided that this copyright notice is retained. //
// //
// RepeatedDigits.java (Chapter 5, page 198) //
//////////////////////////////////////////////////////////////
// Checks a number for repeated digits
import jpb.*;
public class RepeatedDigits {
public static void main(String[] args) {
// Prompt user to enter a number and convert to int form
SimpleIO.prompt("Enter a number: ");
String userInput = SimpleIO.readLine().trim();
int number = Integer.parseInt(userInput);
// Create an array to store digit counts
int[] digitCounts = new int[10];
// Remove digits from the number, one by one, and
// increment the corresponding array element
while (number > 0) {
digitCounts[number%10]++;
number /= 10;
}
// Create a string containing all repeated digits
String repeatedDigits = "";
for (int i = 0; i < digitCounts.length; i++)
if (digitCounts[i] > 1)
repeatedDigits += i + " ";
// Display repeated digits. If no digits are repeated,
// display "No repeated digits".
if (repeatedDigits.length() > 0)
System.out.println("Repeated digits: " +
repeatedDigits);
else
System.out.println("No repeated digits");
}
}
Explanation / Answer
please rate - thanks
//////////////////////////////////////////////////////////////
// From JAVA PROGRAMMING: FROM THE BEGINNING, by K. N. King //
// Copyright (c) 2000 W. W. Norton & Company, Inc. //
// All rights reserved. //
// This program may be freely distributed for class use, //
// provided that this copyright notice is retained. //
// //
// RepeatedDigits.java (Chapter 5, page 198) //
//////////////////////////////////////////////////////////////
// Checks a number for repeated digits
import jpb.*;
public class RepeatedDigits {
public static void main(String[] args) {
// Prompt user to enter a number and convert to int form
SimpleIO.prompt("Enter a number: ");
String userInput = SimpleIO.readLine().trim();
int number = Integer.parseInt(userInput);
while(number>0) //do until sentinel entered
{
// Create an array to store digit counts
int[] digitCounts = new int[10];
// Remove digits from the number, one by one, and
// increment the corresponding array element
while (number > 0) {
digitCounts[number%10]++;
number /= 10;
}
// Create a string containing all repeated digits
String repeatedDigits = "";
for (int i = 0; i < digitCounts.length; i++)
if (digitCounts[i] > 1)
repeatedDigits += i + " ";
// Display repeated digits. If no digits are repeated,
// display "No repeated digits".
if (repeatedDigits.length() > 0)
System.out.println("Repeated digits: " +
repeatedDigits);
else
System.out.println("No repeated digits");
SimpleIO.prompt("Enter a number: ");
userInput = SimpleIO.readLine().trim(); //get next number
number = Integer.parseInt(userInput);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.