13 Replies - 3018 Views - Last Post: 28 January 2010 - 05:06 PM Rate Topic: -----

#1 davewhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-September 09

Help with english to morse code program, I have no idea what I'm d

Post icon  Posted 28 January 2010 - 01:29 AM

Hi

import java.io.*;

public class MorseCode {

public static void main(String[] args){
	BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); 
   
	
   String[] morse = {".- ","-... ","-.-/>. ","-.. ",". ","..-. ","--. ",".... ",".. ",
   ".--- ","-.-/> ",".-.. ","-- ","-. ","--- ",".--. ","--.-/> ",".-. ","... ","- ","..- ",
   "...- ",".-- ","-..- ","-.-/>- ","--.. "};
   String[] letter = {"A","B","C ","D","E","F","G","H","I",
   "J","K","L","M","N ","O","P","Q","R","S","T","U",
   "V","W","X","Y ","Z"}; 
   
   
   int y=0;
	String val="";
   try  { 
	  
	   System.out.println("Input: ");  
	   val = BR.readLine();  
	   int x =Integer.parseInt(val);
	   
	  }catch(Exception e){	   
				System.out.println("error");	   
		   }	
   
   
	switch(y){
			
			 case 1:System.out.println(morse[0]);	   
					  break;	   
			 case 2:System.out.println(morse[1]);	  
					  break;	   
			 case 3:System.out.println(morse[2]);	   
					  break;	   
			 case 4:System.out.println(morse[3]);	  
					  break;	   
			 case 5:System.out.println(morse[4]);	  
					  break;	   
			 case 6:System.out.println(morse[5]);	  
					  break;	   
			 case 7:System.out.println(morse[6]);	   
					  break;	   
			 case 8:System.out.println(morse[7]);	   
					  break;	   
			 case 9:System.out.println(morse[8]);	
					  break;	   
			 case 10:System.out.println(morse[9]);	   
					  break;	   
			 case 11:System.out.println(morse[10]);	  
					  break;	   
			 case 12:System.out.println(morse[11]);	  
					  break;	   
			 case 13:System.out.println(morse[12]);	   
					  break;	   
			 case 14:System.out.println(morse[13]);	 
					  break;	   
			 case 15:System.out.println(morse[14]);	   
					  break;	   
			 case 16:System.out.println(morse[15]);	  
					  break;	   
			 case 17:System.out.println(morse[16]);	  
					  break;	   
			 case 18:System.out.println(morse[17]);	   
					  break;	   
			 case 19:System.out.println(morse[18]);	 
					  break;	   
			 case 20:System.out.println(morse[19]);	   
					  break;	   
			 case 21:System.out.println(morse[20]);	  
					  break;	   
			 case 22:System.out.println(morse[21]);	  
					  break;	   
			 case 23:System.out.println(morse[22]);	   
					  break;	   
			 case 24:System.out.println(morse[23]);	   
					  break;	   
			 case 25:System.out.println(morse[24]);	   
					  break;	   
			 case 26:System.out.println(morse[25]);	   
					  break;	   
			}
	
   
  
		   
	
	
	
	
	
	
	
	
	
}

}


here is the output

compile-single:
run-single:
Input: 
QUICK BROWN FOX
error
BUILD SUCCESSFUL (total time: 50 seconds)




I know there are tons of errors, I need your advice. Please, this is due in 5 hours.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Help with english to morse code program, I have no idea what I'm d

#2 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 661
  • Joined: 20-March 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 01:53 AM

Due in 5hours? That blows.

If your goal is to convert english to morse code, why are you converting the string to Integer here?
System.out.println("Input: ");  
val = BR.readLine();  
int x =Integer.parseInt(val);


This post has been edited by 333OnlyHalfEvil: 28 January 2010 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 davewhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-September 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:19 AM

View Post333OnlyHalfEvil, on 28 Jan, 2010 - 12:53 AM, said:

Due in 5hours? That blows.

If your goal is to convert english to morse code, why are you converting the string to Integer here?
System.out.println("Input: ");  
val = BR.readLine();  
int x =Integer.parseInt(val);



Should I convert it to a character?

This post has been edited by davewhite: 28 January 2010 - 02:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,790
  • Joined: 20-September 08

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:21 AM

What you need is something like the following

		val = BR.readLine();
		for(int i = 0;i < val.length();i++) {	
		String m = morse[val.charAt(0) - 'A'];
		System.out.print(m);
		}


Was This Post Helpful? 0
  • +
  • -

#5 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 661
  • Joined: 20-March 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:23 AM

I'm assuming that your input is an english word and your program is supposed to convert it to morse code. If your input is in english than you don't need to convert it to anything. Converting it to a character doesn't make sense because it's already a character.
Was This Post Helpful? 0
  • +
  • -

#6 davewhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-September 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 01:08 PM

I got the program running.It works fine, But is there any other way other than switch statement?

   import java.io.*;

public class MorseCode {

public static void main(String[] args){
	BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); 
   
	
   String[] morse = {".- ","-... ","-.-/>. ","-.. ",". ","..-. ","--. ",".... ",".. ",
   ".--- ","-.-/> ",".-.. ","-- ","-. ","--- ",".--. ","--.-/> ",".-. ","... ","- ","..- ",
   "...- ",".-- ","-..- ","-.-/>- ","--.. "};
  
   
   

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

String sentence = "";
System.out.println("Enter a sentence");
try{
sentence = dataIn.readLine();
}catch(Exception e){
System.out.println("Invalid Input");
}

for(int x = 0; x<sentence.length(); x++){

char ch = sentence.charAt(x);


 


switch(ch){
case 'a':
case 'A':
System.out.println(morse[0]); break;

case 'b':
case 'B':
System.out.print(morse[1]); break;

case 'c':
case 'C':
System.out.print(morse[2]); break;

case 'd':
case 'D':
System.out.print(morse[3]); break;

case 'e':
case 'E':
System.out.print(morse[4]); break;

case 'f':
case 'F':
System.out.print(morse[5]); break;

case 'g':
case 'G':
System.out.print(morse[6]); break;

case 'h':
case 'H':
System.out.print(morse[7]); break;

case 'i':
case 'I':
System.out.print(morse[8]); break;

case 'j':
case 'J':
System.out.print(morse[9]); break;

case 'k':
case 'K':
System.out.print(morse[10]); break;

case 'l':
case 'L':
System.out.print(morse[11]); break;

case 'm':
case 'M':
System.out.print(morse[12]); break;

case 'n':
case 'N':
System.out.print(morse[13]); break;

case 'o':
case 'O':
System.out.print(morse[14]); break;

case 'p':
case 'P':
System.out.print(morse[15]); break;

case 'q':
case 'Q':
System.out.print(morse[16]); break;

case 'r':
case 'R':
System.out.print(morse[17]); break;

case 's':
case 'S':
System.out.print(morse[18]); break;

case 't':
case 'T':
System.out.print(morse[19]); break;

case 'u':
case 'U':
System.out.print(morse[20]); break;

case 'v':
case 'V':
System.out.print(morse[21]); break;

case 'w':
case 'W':
System.out.print(morse[22]); break;

case 'x':
case 'X':
System.out.print(morse[23]); break;

case 'y':
case 'Y':
System.out.print(morse[24]); break;

case 'z':
case 'Z':
System.out.print(morse[25]); break;
}
}   
System.out.println(".");

}

} 

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:07 PM

You can use if statements; but in this case, switch provides a little more organization. Also, you might want to work on your code indentations a little, as the code you just posted is very difficult to follow.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,790
  • Joined: 20-September 08

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:28 PM

Quote

But is there any other way other than switch statement?


I've already posted code that doesn't use switch
Was This Post Helpful? 0
  • +
  • -

#9 davewhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-September 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:46 PM

[quote name='g00se' date='28 Jan, 2010 - 01:28 PM' post='907778']

Quote

I've already posted code that doesn't use switch



import java.io.*;

public class MorseCode {

public static void main(String[] args){
	BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); 
   
	
   String[] morse = {".- ","-... ","-.-/>. ","-.. ",". ","..-. ","--. ",".... ",".. ",
   ".--- ","-.-/> ",".-.. ","-- ","-. ","--- ",".--. ","--.-/> ",".-. ","... ","- ","..- ",
   "...- ",".-- ","-..- ","-.-/>- ","--.. "};
  
	
   
String sentence = "";
System.out.println("Enter a sentence: ");
try{
sentence = dataIn.readLine();
}catch(Exception e){
System.out.println("Invalid Input");
}

		for(int i = 0;i < sentence.length();i++) {	
		String m = morse[sentence.charAt(0) - 'A'];
		System.out.print(m);
		}



  

}
}


It does not work.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,790
  • Joined: 20-September 08

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 02:53 PM

Quote

It does not work.


Works fine for me
Was This Post Helpful? 0
  • +
  • -

#11 davewhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-September 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 03:06 PM

Quote

Works fine for me

deps-jar:
compile-single:
run-single:
Enter a sentence: 
I AM NOT LYING
.. .. .. .. .. .. .. .. .. .. .. .. .. .. 
BUILD SUCCESSFUL (total time: 4 seconds)



I'm dead

This post has been edited by davewhite: 28 January 2010 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,790
  • Joined: 20-September 08

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 03:14 PM

Sorry - didn't test it properly - hang on
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,790
  • Joined: 20-September 08

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 03:19 PM

Try this:

	for (int i = 0; i < sentence.length(); i++) {
		char c = sentence.charAt(i);
		if(Character.isLetter(c)) {
		String m = morse[c - 'A'];
		System.out.print(m);
		}
	}



You can make that a little more robust with
char c = Character.toUpperCase(sentence.charAt(i));


This post has been edited by g00se: 28 January 2010 - 03:22 PM

Was This Post Helpful? 1
  • +
  • -

#14 davewhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-September 09

Re: Help with english to morse code program, I have no idea what I'm d

Posted 28 January 2010 - 05:06 PM

Why won't it read space?
import java.io.*;

public class MorseCode {

public static void main(String[] args){
	BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); 
   
	
   String[] morse = {".- ","-... ","-.-/>. ","-.. ",". ","..-. ","--. ",".... ",".. ",
   ".--- ","-.-/> ",".-.. ","-- ","-. ","--- ",".--. ","--.-/> ",".-. ","... ","- ","..- ",
   "...- ",".-- ","-..- ","-.-/>- ","--.. "," "};
  
	   String sentence = "";
	   System.out.println("Enter a sentence: ");
	  try{
		  sentence = dataIn.readLine();
	  }catch(Exception e){
		  System.out.println("Invalid Input");
	   }


  
		  for (int i = 0; i < sentence.length(); i++) {
			   char letter = Character.toUpperCase(sentence.charAt(i));
				  String word = morse[letter - 'A'];
				  System.out.print(word+".");
			   }
		 }

	   }





Enter a sentence: 
G G
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -33
		at MorseCode.main(MorseCode.java:25)
--. .
Java Result: 1
BUILD SUCCESSFUL (total time: 28 seconds)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1