Given a string input, write a program to replace every appearance of the word “is” with “is not”. If the word “is” is immediately preceded or followed by a letter no change should be made to the string.
Input and Output Format
- Input consists of a String.
- The output consists of a String.
Sample Input 1:
This is just a misconception
Sample Output 1:
This is not just a misconception
Sample Input 2:
Today is misty
Sample Output 2:
Today is not misty
Convert a string into the negative string in Java
Following are the steps to convert a string into negative:
- Input string from the user.
- Now, call the replaceAll() method and replace the word “is” with “is not” and store it in the new variable of string type.
- At last, print the string.
public class Sort { public static void main(String [] args) { Scanner sc= new Scanner(System.in); String str=sc.nextLine(); String n=str.replaceAll(" is ", " is not "); System.out.println(n); } }
Output
Convert String to Integer Value
Write a Java Program to convert a string into Integer. This is the most common thing we have to do when we create a web application and get the value from the frontend to the backend.
For example: When you create an HTML input field all the information would be stored in strings form now, if you want to convert them into integers then at the server side you can use the following ways.
- Integer.parseInt(): It would convert string to a primitive int value.
- Interger.valueOf(): It would wrap the string to Interger class.
Input and Output Format
- Input consists of a String.
- The output consists of an integer value.
Sample Input 1:
786
Sample Output 1:
Using one way 786
Using another way 786
Following are the steps to convert string to Integer:
- Input string from the user.
- Pass it to Inetger.parseInt() and Interger.valueOf() method and hence print them.
package com.demo; import java.util.Scanner; public class TestJava2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); // Using Integer.parseInt() method int a = Integer.parseInt(str); System.out.println("Using one way "+a); Integer b = Integer.valueOf(str); System.out.println("Using another way "+b); } }
Output
Thus, in this way, we learn how to convert a string into a negative string using the replaceAll() method.