Question

WRITE A JAVA PROGRAM . (WILL GUARANTEE A THUMBS UP) BELOW ARE INSTRUCTIONS: IF USER ENTERS:...

WRITE A JAVA PROGRAM . (WILL GUARANTEE A THUMBS UP)

BELOW ARE INSTRUCTIONS:

IF USER ENTERS:

"1, 2, 3"

THIS IS THE Expected Output:

1,1,2,2,3,3

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

Here the solution for above problem in java. I have used print method in java to print output in same line, also for to avoid comma in last element i ran the loop till second last element

CODE IN JAVA


import java.util.*;
import java.io.*;
public class Main
{
   public static void main(String[] args) {
//SCANNING FOR INPUT  
       Scanner sc = new Scanner(System.in);
String line = sc.next();
//SPLITING THE STRING BASED ON COMMAN
String[] vector = line.split(",");
int x;
int i;
//ITERATING THROUGH ALL NUMBER PRESENT
for(i=0;i<vector.length-1;++i)
{
//PARSING THE INTEGER X
x=Integer.parseInt(vector[i]);
//PRINTING THE VALUE ON CONSOLE PRINT METHOD IS USED AS IT DOES NOT LEAVE NEW LINE
System.out.print(x);
System.out.print(",");
System.out.print(x);
System.out.print(",");
}
//FOR LAST ELEMENT IGNORING THE LAST COMMA
x=Integer.parseInt(vector[i]);
System.out.print(x);
System.out.print(",");
System.out.print(x);
System.out.println("");
  
   }
}
OUTPUT OF CODE

SCREENSHOT OF CODE

Add a comment
Know the answer?
Add Answer to:
WRITE A JAVA PROGRAM . (WILL GUARANTEE A THUMBS UP) BELOW ARE INSTRUCTIONS: IF USER ENTERS:...
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
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