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++!
Start by first: Write the code necessary to accept a date in the format (Alice:November,4,2020) and...
Start by first: Write the code necessary to accept a date in the format (Alice:November,4,2020) and store it in a structure called birthday. (Define the structure, declare the variable, ask the user for input, and store the values in the structure. The month should be a character array, the day and year should be integers.) Then, Declare a pointer name bdayptr to the type of structure you defined in the previous question. Have the pointer point to the structure you...
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...
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
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 C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT