Question

In: Computer Science

Write a java code that first discards as many whitespace characters as necessary until the first...

Write a java code that first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in the string is not a valid integral number, or if no such sequence exists because either string is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Example 1:

Input: "42"

Output: 42

Example 2:

Input: "   -42"

Output: -42

Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"

Output: 4193

Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"

Output: 0

Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.

Note 1: Place single line comments wherever it is necessary before a line of code to explain the lines of code in Java Project.

Note 2: When you are testing your code, drop the " around the input values.

Note 3: In this project, you should use the String methods you have learned in this class and implement a loop structure.  

Solutions

Expert Solution

Below is code:

import java.util.*;

public class Main
{
        public static void main(String[] args) {
            String isNext = "y";
            Scanner sc= new Scanner(System.in); 
          
            //define regular expression
            String regex = "(\\s)[^\\d]+";
            
            //loop structure. 
            while(isNext.equals("y"))
            {
                System.out.print("Input: ");
                //take input from user
                String str= sc.nextLine(); 
                
                //remove double quots and spaces from starting
                str = str.replace("\"", "");
                str = str.trim();
                
                //get number from string with matches regular expression
            String[] str1 = str.split(regex);
            try {
                //print numerical digit.
                System.out.println("Output: "+Integer.parseInt(str1[0]));
            } catch (NumberFormatException e) {
                
                //othervise print 0
                System.out.println("Output: "+0);
            }
            System.out.print("Do you want to continue? (y/n): ");
            isNext= sc.nextLine(); 
            }
            
        }
}

Output:


Related Solutions

what are three characters that are subject to whitespace collapsing?
what are three characters that are subject to whitespace collapsing?
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
how to code the text with excess whitespace removed in C++. Whitespace includes spaces and tabs....
how to code the text with excess whitespace removed in C++. Whitespace includes spaces and tabs. Excess whitespace is: • All whitespace at the beginning of the text. • All whitespace at the end of the text. • Any point in the text where more than a single whitespace character is used. For instance, if the input text is: Welcome to C++ ! The output would be: Welcome to C++!
Write in Java. Separate code for each question. The answer to the first question should NOT...
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable. 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of...
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Write a java code to find the following. For numbers 501 to 999, how many numbers...
Write a java code to find the following. For numbers 501 to 999, how many numbers will have the sum of the digits equal to 10. 501, sum of digits=6 502, sum of digits=7 503, sum of digits=8 504, sum of digits=9 505, sum of digits=10 506, sum of digits=11
Write a Java program that will first ask the user how many grades they want to...
Write a Java program that will first ask the user how many grades they want to enter. Then use a do…while loop to populate an array of that size with grades entered by the user. Then sort the array. In a for loop read through that array, display the grades and total the grades. After the loop, calculate the average of those grades and display that average. Specifications Prompt the user for the number of grades they would like to...
write a java program. 10-20 lines of code You are a landscaper, one of your first...
write a java program. 10-20 lines of code You are a landscaper, one of your first tasks is to determine the cost of landscaping a yard. You charge a flat fee of $40 to landscape a yard, and an extra $10 a bag for raking leaves. Not all yards you work on have leaves that need to be raked. Houses without any leaves will be discounted $5. Using your program, use the JOption Pane to ask the homeowner to enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT