Question: We are assigned to do a project, and I finished all parts but when I was going over it, and make sure it works as

We are assigned to do a project, and I finished all parts but when I was going over it, and make sure it works as it supposes to work found an issue that I could not solve. My code is only working with one String in like 12345, but not with 1 2 3 4 5. can you help me modify my code, so it operates on both.

The program should prompt the user to enter a string consisting of zero or more digits in non-decreasing order separated by spaces be sure to read this in as a single string. Non-decreasing order means sorted with duplicates allowed. For example, the user might enter the string 2 2 5 8 (of course the user does not type the quotes). Or the user might enter the empty string (by simply pressing the enter key at the prompt) since he/she was prompted to enter zero or more digits in non-decreasing order. Also, the user can enter the letter q by itself to quit the program.

Next, your program should generate a random digit and inform the user what digit was generated. For example, if the random digit was a 5 then program might display The following digit was randomly generated at great expense: 5.

Next, the program should construct a string identical to the string that the user entered but with the randomly-generated digit inserted into a correct position so that the resulting string is still in non-decreasing order. For example, if the user entered 2 2 5 8 and the randomly-generated digit was 5, then the new string should be 2 2 5 5 8. Of course if the user entered the empty string and the randomly-generated digit was 7, then the new string would just be 7. The program should output the new string.

Be sure to test your program thoroughly.

Here is an example run of the program.

Please enter zero or more digits in non-decreasing order (or q to quit): 2 2 5 8 The following digit was randomly generated at great expense: 5 Here is the new sequence including the random digit: 2 2 5 5 8 Please enter zero or more digits in non-decreasing order (or q to quit): The following digit was randomly generated at great expense: 7 Here is the new sequence including the random digit: 7 Please enter zero or more digits in non-decreasing order, (or q to quit): q Have a nice day!

import java.util.Scanner;

public class OneMoreDigit { public static void main (String[]args) { Scanner input = new Scanner(System.in);

String digits = ""; boolean countinue = true; boolean isValid = true; do {

System.out.print(" Please enter zero or more digits in non-decreasing order (or q to quit): ");

digits = input.nextLine();

countinue = true; isValid = true; if( digits.equals("q") || digits.equals("Q") ){ System.out.println("Have a nice day "); countinue = false; System.exit(0); } if( digits.equals(" ") ){ System.out.print(" "); } else { for(int i = 0 ; i < digits.length(); i++) if( digits.charAt(i) >= 48 && digits.charAt(i) <= 57){ }else{ isValid = false; } for( int i = 1 ; i < digits.length() ; i++){ if( digits.charAt(i - 1) > digits.charAt(i)) isValid = false; } }

if(isValid){ String newDigits = ""; int Rand = 0 + (int) (Math.random() * (9 - 0 ) ); System.out.println( "The following digit was randomliy genearted at great expense:" + Rand) ;

boolean first = true; for( int i = 0 ; i < digits.length() ; i ++ ){

if( digits.charAt(i) < Rand+48){ newDigits += digits.charAt(i); }else{ if(first){ newDigits += Rand; newDigits += digits.charAt(i); first = false; } else newDigits += digits.charAt(i); } } if(first){ newDigits += Rand; } System.out.println("Here is the new sequence including the random digit:" + newDigits); } else { System.out.println("error"); }

} while(countinue); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!