In: Computer Science
Word to Digit Programming challenge description:
Given a string representation of a set of numbers, print the digit representation of the numbers.
Input: Your program should read lines from standard input. Each line contains a list of word representations of numbers separated by a semicolon. There are up to 20 numbers in one line. The numbers are "zero" through "nine".
Output: Print the sequence of digits. Test 1 Input zero;two;five;seven;eight;four Expected Test 1 output 025784 Test 2 Input three;seven;eight;nine;two Expected Output 37892
PLEASE USE JAVA:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class Main {
/**
* Iterate through each line of input.
*/
public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in,
StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
String line;
while ((line = in.readLine()) != null) {
///CODE GOES HERE
System.out.println(line);
}
}
}
Code
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class Main{
/**
* Iterate through each line of input.
*/
public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
String line;
while ((line = in.readLine()) != null)
{
String a[] = line.split(";");
String res="";
for(int i=0;i<a.length;i++)
{
if(a[i].equals("one"))
res+="1";
if(a[i].equals("two"))
res+="2";
if(a[i].equals("three"))
res+="3";
if(a[i].equals("four"))
res+="4";
if(a[i].equals("five"))
res+="5";
if(a[i].equals("six"))
res+="6";
if(a[i].equals("seven"))
res+="7";
if(a[i].equals("eight"))
res+="8";
if(a[i].equals("nine"))
res+="9";
if(a[i].equals("zero"))
res+="0";
}
System.out.println(res);
}
}
}
Terminal Work
.