Question

IN JAVA Write a program that, given an integer, inserts a '*' between adjacent digits that...

IN JAVA

Write a program that, given an integer, inserts a '*' between adjacent digits that are both even and a '-' between adjacent digits that are both odd. Zero should not be considered even or odd.

EX:

12467930

Expected Output

12*4*67-9-30

Homework Answers

Answer #1
import java.util.Scanner;

public class InsertSymbols {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = "";
        int d1, d2;
        for (int i = 0; i < s.length(); i++) {
            result += s.charAt(i);
            if (i < s.length()-1) {
                d1 = s.charAt(i)-'0';
                d2 = s.charAt(i+1)-'0';
                if (d1%2 == 0 && d2%2 == 0) {
                    result += '*';
                }
                if (d1%2 == 1 && d2%2 == 1) {
                    result += '-';
                }
            }
        }
        System.out.println(result);
    }
}

Know the answer?
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
Similar Questions
Using for loops: - write a Java program that adds numbers between 1 to 100 and...
Using for loops: - write a Java program that adds numbers between 1 to 100 and prints the result. - write a Java program that adds odd numbers between 1 to 100 and prints the result - write a Java program that averages even numbers between 5 to 30 and prints the result
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
Write a C++ program to prompt a user for a positive integer and print the number...
Write a C++ program to prompt a user for a positive integer and print the number of even and odd digits (0 is even). Sample run: Enter a positive integer: -4580 Invalid. Enter a positive integer: 12146 2 odd digits 3 even digits
Write a C++ program to read a positive integer greater than 0, the program should compute...
Write a C++ program to read a positive integer greater than 0, the program should compute and display the following: - Sum of odd digits in the number - Count of even digits in the number - The smallest digit.                    For example, if the input is 24536, then Sum of odd digits in the number = 8 Count of even digits in the number = 3 Smallest digit = 2
given an integer, write a program that displays the number as follows first line         all digits...
given an integer, write a program that displays the number as follows first line         all digits second line   all except first digit third line        all except first two digits ..... last line          the last digit ex) 5 6 7 8 6 7 8 7 8 8
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
Write a java program to display a given range of integer numbers (0 to 9) in...
Write a java program to display a given range of integer numbers (0 to 9) in the following order: Example : given (5 to 9), the program displays 56789 67895 78956 89567 95678 The first line displays the min to max range. Each line that follows should display the next higher number except when it hits the maximum number. In that situation, the line wraps back to the minimum sequence. For this problem write a static method to do the...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
Challenge 1 - SumDigits.java Write a program that reads an integer between 0 and 1000 and...
Challenge 1 - SumDigits.java Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 837, the sum of all digits is 18. Sample output: Enter an integer between 0 and 1000: 837 The sum of all digits in 837 is 18
write a C program to find whether the given number of three digits is an integer...
write a C program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or -999 to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or -999 to quit : 371 3**3 + 7**3 + 1**3 is = 371...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT