Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Scanner Object Scanner,java 3 36 // TOD0 (1e) Add code to the program to do the

ID: 3742648 • Letter: S

Question

Scanner Object



Scanner,java 3 36 // TOD0 (1e) Add code to the program to do the opposite. That is, you'll now now need 37 to prompt your user for a number between 1,000 and 999,999 (thistime, without the comma). Then you'll write the code to display the user's number with the comma at the appropriate Location. For example, if the user enters 123456 then the output here would be: 123,456 39 41 4 43 44 (2) MODIFIED PHRASE 45 // TODO (2a) 47 48 49 50 51 52 53 54 Write some code to prompt the user to enter a sentence or phrase. Thern ask them for two letters: the first will be the letter in their sentence/ phrase to be replaced, the second will be the letter that will do the replacing. Once you've finished asking for and reading this information, print out the newly modified sentence/phrase with the appropriate letters replaced For example, your output might show: Enter a phrase: There are 10 kinds of people in the world Enter the letter to be replaced o Enter the letter you want the o to be replaced by: a The new phrase is: There are 10 kinds of people in the world 56 8 (3) PRACTICE PROBLEM 9 e II TODO (3a) Generate some random numbers using Math.random ()

Explanation / Answer

Number Format:

import java.text.NumberFormat;
import java.util.Locale;

public class Scanner {
   public static void main(String[] args) {
       int n; //create integer variable
       java.util.Scanner in = new java.util.Scanner(System.in); //scanner object
       System.out.print("Enter number between 1000 to 999999: "); //ask user to input number
       n = in.nextInt(); //input nuber
       if (n >= 1000 && n <= 999999) { //check number entered between 1000 to 999999
           NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); //formamt class to format nuber in us
           String numberAsString = numberFormat.format(n); //format input integer number
           System.out.println("Entered number is: "+numberAsString); //display number formatted
           System.exit(0);
       } else {
           System.out.println("Wrong input! "); //error
       }
   }
}

sample output:

Enter number between 1000 to 999999: 123456
Entered number is: 123,456

Enter number between 1000 to 999999: 1000000
Wrong input!

String replace:

public class Scanner {
   public static void main(String[] args) {
       java.util.Scanner in = new java.util.Scanner(System.in); //scanner object
       String str, replaceStr, replaceStrBy,newStr;
       System.out.println("Enter a phrase: "); //ask user to input string
       str = in.nextLine(); //input string
      
       java.util.Scanner inStr = new java.util.Scanner(System.in); //scanner object
       System.out.print("Enter the letter to be replaced: "); //ask user to input string
       replaceStr = inStr.next(); //input string
      
       java.util.Scanner inStrBy = new java.util.Scanner(System.in); //scanner object
       System.out.print("Enter the letter you want the "+ replaceStr +" to be replced by: "); //ask user to input string
       replaceStrBy = inStrBy.next(); //input string
      
       newStr = str.replace(replaceStr, replaceStrBy); //replace string
      
       System.out.print("The phrase is: "+newStr); //print phrase
   }
}

sample output:

Enter a phrase:
There are 10 kinds of people in the world
Enter the letter to be replaced: o
Enter the letter you want the o to be replced by: a
The new phrase is: There are 10 kinds af peaple in the warld

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote