Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: My lines won't work!!!

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My lines won't work!!!

    Hi every one,

    i'm new to java programming. I just started using Sams teach yourself Java in 21 days. I've just completed day 4th but i can't solve the second exercise which was to create a class that takes words for the first 10 numbers ("one" up to "ten") and converts them into a single long integer by using a switch statement for the conversion and command-line arguments for the words. I found the soluce on the book's web site but i want the code i wrote to work for i know there isn't only one single way to solve the problem. Here i send you my codes, can you please tell me what went wrong. Thank you.
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: My lines won't work!!!

    please tell me what went wrong.
    Its better to post the code so all can read vs attaching a file.
    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.

  3. #3
    Junior Member Jelula's Avatar
    Join Date
    Jun 2010
    Location
    Tampa, FL
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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:
    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.
    All truly great thoughts are conceived while walking. - Nietzsche

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My lines won't work!!!

    I wanted to give it a go...hope it helps you

    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 >
    Programmer - an organism that turns coffee into software.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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.

    where the program is reading in the string from.
    The String is coming from the command line arguments passed to main().

  6. The Following User Says Thank You to Norm For This Useful Post:

    Jelula (July 2nd, 2010)

  7. #6
    Junior Member Jelula's Avatar
    Join Date
    Jun 2010
    Location
    Tampa, FL
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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.
    All truly great thoughts are conceived while walking. - Nietzsche

Similar Threads

  1. Drawing circles with smoother lines?
    By tabutcher in forum Java Theory & Questions
    Replies: 4
    Last Post: April 18th, 2010, 10:12 AM
  2. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 06:49 PM
  3. [SOLVED] Supressing empty lines and does not do until the end! Why ?
    By lumpy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 06:38 AM
  4. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 06:55 PM
  5. How to assign values from a file to an arraylist of objects?
    By nadman123 in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2008, 01:07 PM