Re: My lines won't work!!!
Quote:
please tell me what went wrong.
Its better to post the code so all can read vs attaching a file.
Quote:
tell me what went wrong.
Can you tell us why you're not happy with the output from the program? Show the output and explain.
Re: My lines won't work!!!
In case anyone didn't want to open the text file (Which was a pain to read) I fixed it up a little.
Here it is:
Code :
public class WordToNumber {
public static void main (String[] arguments) {
long num = 0;
String str1 = "zero";
if (arguments.length > 0) {
str1 = (String)arguments[0];
for (int i = 0; i < arguments.length; i++) {
switch (i) { //***This confuses me here. Didn't you just use the i in the line above?
case 1:
if (str1 == "one") {
num = 1L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 2:
if (str1 == "two") {
num = 2L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 3:
if (str1 == "three") {
num = 3L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 4:
if (str1 == "four") {
num = 4L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 5:
if (str1 == "five") {
num = 5L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 6:
if (str1 == "six") {
num = 6L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 7:
if (str1 == "seven") {
num = 7L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 8:
if (str1 == "eigth") {
num = 8L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 9:
if (str1 == "nine") {
num = 9L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
case 10:
if (str1 == "ten") {
num = 10L;
System.out.println(str1 + " is equivalent to " + num);
}
break;
default:
if (str1 == "zero") {
num = 0L;
System.out.println(str1 + " is equivalent to " + num);
}
}
}
}
}
}
What I don't understand (And I'm a bit of a noob so maybe you can explain this to me OP) is where the program is reading in the string from. It also looks to me like the switch is running off of the i you're using for the FOR loop? I could just be completely wrong/confused though.
Re: My lines won't work!!!
I wanted to give it a go...hope it helps you
Code :
import java.io.*;
public class wordToLong
{
public static void main(String[] args)
{
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
PrintWriter stdOut = new PrintWriter(System.out, true);
stdOut.print("Input string : ");
stdOut.flush();
try
{
String all_words = stdIn.readLine();
String[] words = all_words.split(" ");
String digits = new String();
for(int i=0;i<words.length;++i)
{
switch(words[i].charAt(0))
{
case 'o':
digits = digits.concat("1");
break;
case 't':
switch(words[i].charAt(1))
{
case 'w':
digits = digits.concat("2");
break;
case 'h':
digits = digits.concat("3");
break;
case 'e':
digits = digits.concat("10");
break;
}
break;
case 'f':
switch(words[i].charAt(1))
{
case 'o':
digits = digits.concat("4");
break;
case 'i':
digits = digits.concat("5");
break;
}
break;
case 's':
switch(words[i].charAt(1))
{
case 'i':
digits = digits.concat("6");
break;
case 'e':
digits = digits.concat("7");
break;
}
break;
case 'e':
digits = digits.concat("8");
break;
case 'n':
digits = digits.concat("9");
break;
}
}
long theFinalLong = Long.parseLong(digits);
stdOut.println("< " + theFinalLong + " >");
}
catch(IOException e)
{
stdOut.println("Error : " + e.getMessage());
}
finally
{
try
{
stdIn.close();
stdOut.close();
}
catch(IOException e)
{
System.out.println("Cannot close streams !");
}
}
}
}
Sample runs:
Input : one three six five five
Output : < 13655 >
Re: My lines won't work!!!
Not much error checking. Any word beginning with o is a 1 etc.
But given the requirements to use a switch statement there probably isn't much choice.
You could add a test to see if the String starting with 'o' was 'one' and give an error message.
Quote:
where the program is reading in the string from.
The String is coming from the command line arguments passed to main().
Re: My lines won't work!!!
Ah okay. Sorry if that seemed like a stupid question, haven't coded anything like that in the narrow scope of the class I'm in.