Question

In the first task, you will write a Java program that accepts a string of the...

In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end.

1. Prompt the user to enter a string with the specific format (dddxxdddxx*)

2. Read the user input as a String

3. The user input will contain two numbers. The first number will start at index 0 and has three digits and the second number will start at the index 5 and has three digits.

4. Extract the two numbers using the substring method available in String class. This will give you two Strings that contain numbers.

5. Covert the two Strings obtained in the last step to integers using the parseInt method in the Integer class.

6. Add the two numbers in the user input and divide the result by the length of the input string.

7. Output the result including the fractional part of the answer.

For example, if the user input is “1203x020e45A5524” then the two number strings will be “120” and “020”. The sum should be 140 and the answer displayed should be 8.75 (140/16).

”. Prompt the user to enter a string in the given format, read the input, process it and show the answer. A possible dialogue could be:

Please enter a String with two three-digit numbers at the beginning and at the index 5 (first three characters and characters from 6 to 8 must be numbers, for example 123ab678as343fdfd): 200w42444wffdf23t4e5

The answer is: 22.2 (444/20)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class StringManip

{

       public static void main(String[] args) {

      

             Scanner scan = new Scanner(System.in);

             String userInput;

             // input of string in the format (dddxxdddxx*)

             System.out.print("Please enter a String with two three-digit numbers at the beginning and at the index 5 (first three\ncharacters and characters from 6 to 8 must be numbers, for example 123ab678as343fdfd): ");

             userInput = scan.next();

            

             int num, den;

             // get the first 3-digit number that will start at index 0

             num = Integer.parseInt(userInput.substring(0, 3));

             // get the second 3-digit number that will start at index 5

             den = Integer.parseInt(userInput.substring(5,8));

            

             // calculate the result by dividing the sum of the above two numbers with the length of the string

             double result = ((double)(num+den))/userInput.length();

             // display the result

             System.out.println("The answer is "+result+" ("+(num+den)+"/"+userInput.length()+")");

                             

             scan.close();

          }

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
In the first task, you will write a Java program that accepts a string of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two...

    c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two interesting programming challenges that will require a bit of character and/or string related processing with functions. (Carefully read the specifications for each programming challenge and select ONE.) (1) Sum of Digits in a String Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program...

  • Write a program in java to read a string object consisting 300 characters or more using...

    Write a program in java to read a string object consisting 300 characters or more using index input Stream reader. The program should perform following operations. The String must have proper words and all kind of characters.(Use String class methods). a, Determine the length of the string count the number of letters in the strings , count the number of numeric object d. Calculate the number of special character e. Compute the ratio of the numeric to the total f....

  • Write a Java program that determines if a person hits the "Pick 3" loterry numbers. Have...

    Write a Java program that determines if a person hits the "Pick 3" loterry numbers. Have the program to randomly create a 3 digit lottery number. Have the user enter the number they will be playing and the results: (DO NOT USE ARRAYS or STRINGS) -If the user input matches the lottery number in the exact order, the award is $5Million. -If all the digits in the user input match all the digits in the lottery number, the award is...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT