09-8-2012, 03:20 PM | #1 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Continuing to fail at Java
So I'm still doing my Java study, which is going surprisingly slowly due to the speed of the rest of the students. Now I have to interest myself somehow.
Trying to make a Java "hiragana-notepad" that dynamically changes my latin character input into hiragana. So far I can't even get a KeyListener to work. tl;dr code inc Using the MVC-model + a main class Main class (not that interesting): GUI class (works properly): HiraganaController class (no object is made, program doesn't even get to the switch): HiraganaConfig class (Just a husk of code, will come later): When I run the program, it starts the GUI fine and I can type text in the JTextArea as much as my heart desires. However, when I type an "a", it doesn't do the System.out.println() described in the controller class. The KeyListener should do a check whenever I type a vowel, I just don't know how to initiate that from within the GUI class... Any help?
__________________
Quote:
Last edited by SKG_Scintill; 09-8-2012 at 04:40 PM.. |
|
09-9-2012, 01:28 AM | #2 |
FFR Simfile Author
|
Re: Continuin to fail at Java
Dumb question but have you tried using KeyReleased instead of KeyTyped?
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
09-9-2012, 05:02 AM | #3 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Well **** me silly, that actually worked xD
I'll keep using this thread in case I stumble upon other things Until I finish the program that is Thanks Patashu!
__________________
Quote:
|
|
09-9-2012, 06:19 AM | #4 | ||
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Can you make a method with a switch() using an int[]?
"int vow" is the KeyCode of a vowel pressed "int prev" is the KeyCode of the consonant before the vowel Code:
public void hirConfig(int prev, int vow){ int[] sym = {prev, vow}; switch(sym){ case {87,65}: System.out.println("わ"); break; } } Quote:
__________________
Quote:
Last edited by SKG_Scintill; 09-9-2012 at 06:22 AM.. |
||
09-9-2012, 07:28 AM | #5 |
FFR Simfile Author
|
Re: Continuin to fail at Java
Don't use a case switch, use a http://docs.oracle.com/javase/1.4.2/...Hashtable.html
For bonus credit, populate the Hashtable from a file instead of in code, so you can alter it without recompiling
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
09-10-2012, 06:48 AM | #6 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Tried another route, and I have a weird NullPointerException
Code:
package controller; import java.awt.event.*; import model.HiraganaConfig; import view.GUI; public class HiraganaController implements KeyListener{ private GUI gui; private HiraganaConfig config; private int prev; public HiraganaController(GUI gui){ this.gui = gui; } public void keyPressed(KeyEvent e){} public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); switch(key){ default: prev = e.getKeyCode(); break; case KeyEvent.VK_A: config.hirConfig(key,prev); break; case KeyEvent.VK_E: config.hirConfig(key,prev); break; case KeyEvent.VK_I: config.hirConfig(key,prev); break; case KeyEvent.VK_O: config.hirConfig(key,prev); break; case KeyEvent.VK_U: config.hirConfig(key,prev); break; } } public void keyTyped(KeyEvent e){} } I put "System.out.println(key);" and "System.out.println(prev);" before "config.hirConfig(key,prev);" and it DOES give the KeyCodes of both B and A. I tried giving the parameters manually: Code:
config.hirConfig(65,66); It says it's in line 25, which is the line I just gave. Not a clue where it is coming from. What is null?
__________________
Quote:
Last edited by SKG_Scintill; 09-10-2012 at 06:51 AM.. |
|
09-10-2012, 08:31 AM | #7 |
FFR Simfile Author
|
Re: Continuin to fail at Java
private HiraganaConfig config;
This variable is null since you never initialize it, and since it's private it couldn't be initialized outside of the file either.
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
09-10-2012, 08:58 AM | #8 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Thankfully it's all small problems :P
__________________
Quote:
|
|
09-10-2012, 09:50 AM | #9 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Back so soon? Yes, quite...
It's pretty much the last step, and it's starting to look like Murphy's Law. So all there's left for me to do is to get the right symbol in the JTextArea (I made two now, code is coming up), except it doesn't set it when I do setText. GUI code: HiraganaConfig code: If I put sys.out's for key and prev in cases 1 and 66 (1 is the default value of prev) it does print them correctly. The only thing not working is the "gui.hirarea.setText();", which is strange as it does work when I put "hirarea.setText();" in the GUI class. Also, setText() isn't really what I need I think, as one symbol would overwrite the other; I want (lol I so demandin') it to type the symbol next to the other. In the end I might just do it all over with .getText() instead of KeyEvent, but I can't just give up now.
__________________
Quote:
|
|
09-10-2012, 03:16 PM | #10 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Triple post, figured it out
Didn't have to make a new GUI in the config class and make the hirarea static Well then... off to make it with a .getText()
__________________
Quote:
|
|
09-11-2012, 08:38 AM | #11 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Finished, had to redo most of the code, those switch/cases were having too many exceptions to work. I made it with a systematic list for the code to run down, much easier.
View: Control: Model: Now I'm off to make it so you can use capslock to convert a substring to Katakana P.S: Is there any way to make code more elegant?
__________________
Quote:
Last edited by SKG_Scintill; 09-11-2012 at 08:47 AM.. |
|
09-11-2012, 04:02 PM | #12 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
I'm the only one using my own thread, I don't mind.
Can you check if a String contains two of the same chars next to eachother? For example: the word "ganbatte" in romaji has to print "がんばって" instead of "がんばtて" which it does now. I could just make it .replace("t","っ"), but I'd have to do it for every consonant... Any help?
__________________
Quote:
|
|
09-11-2012, 04:44 PM | #13 |
FFR Veteran
|
Re: Continuin to fail at Java
That's tricky and not anything Java-specific. What you are doing is known as parsing. Essentially you are writing a parser that converts from romaji to Japanese characters. Without knowing Japanese myself, I'm not going to be any good at telling you the correctness of that particular conversion.
What I will tell you is you need to make sure to handle all your cases. Also it matters in what order things are evaluated. Substitutions which take more precedence over others need to come earlier in the list. Parsing can be a difficult and tricky topic and there are entire programming languages written only for writing parsers. http://en.wikipedia.org/wiki/Parsing |
09-13-2012, 10:42 AM | #14 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Did it with a for loop:
Code:
for(int x = 2; x < input.toCharArray().length + 1; x++){ if(input.substring(x-2,x-1).equals(input.substring(x-1,x))){ String sub = input.substring(x-2,x); input = input.replace(sub,"っ" + sub.charAt(0)); } } Code:
for(int x = 4; x < input.toCharArray().length + 1; x++){ if(input.substring(x-2,x-1).equals(input.substring(x-1,x))){ String sub = input.substring(x-2,x); input = input.replace(sub,"っ" + sub.charAt(0)); } if(input.substring(x-4,x-2).equals(input.substring(x-2,x))){ String sub = input.substring(x-4,x); input = input.replace(sub,sub.substring(x-2,x) + "ゝ"); } }
__________________
Quote:
Last edited by SKG_Scintill; 09-13-2012 at 10:55 AM.. |
|
09-13-2012, 05:27 PM | #15 |
FFR Simfile Author
|
Re: Continuin to fail at Java
In general it's a bad idea to modify something you're iterating over if it causes its length to change. Scan the input string, building the output string separately (use StringBuilder for efficiency) a character at a time.
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
09-14-2012, 03:01 AM | #16 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
That seems a bit redundant, that way I'd need a StringBuffer in my for-loop.
I checked the StringBuffer and it doesn't have a method that checks all substrings of a given length existing in the String, which is what I made with the for-loop. Nevertheless, I'm only a starter at Java with my "less than half a year of class"-experience (first year only had one trimester Java). If I'm overlooking something important, do tell ;) Edit: I get the idea that "you can better do it the right way the first time, as in the future you may come across problems if you do it this way", but I approach every problem differently and learn experimentally.
__________________
Quote:
Last edited by SKG_Scintill; 09-14-2012 at 03:04 AM.. |
|
09-14-2012, 03:30 AM | #17 | |
FFR Simfile Author
|
Re: Continuin to fail at Java
Quote:
EDIT: Wait, maybe I don't understand the problem yet. If the problem is that you want aaaa to be turned into bb before it notices the first aa and turns it into c, well, why not run the second 'if' statement in a for loop, then run the first 'if' statement in a second for loop?
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png Last edited by Patashu; 09-14-2012 at 03:33 AM.. |
|
09-14-2012, 05:21 AM | #18 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Continuin to fail at Java
Well yeah, making a second for-loop is the easy way out. I'm trying to make code more elegant step by step.
__________________
Quote:
|
|
09-15-2012, 04:53 AM | #19 |
FFR Veteran
|
Re: Continuin to fail at Java
Example:
Just make sure if aaaa is something special, and aa is also something special, that input of aaaa will work correctly. Suppose aaaa -> b Suppose aa -> c Will input of aaaa go to b or will it go to cc? Well, that depends entirely on the order. In this case you probably want the longer match first, i.e. earlier in the list. You just have to make sure it works for every case. Think of possible cases which might trip it up, etc... Not knowing Japanese, I can't tell you what these cases may be in this particular case/etc... but know that order very well may matter and it may be possible to get stuck in infinite loops if two rules mutually depend on each other, or similar. Seriously, hit up that wikipedia link for a background on the subject. Then read the related entries as well. I think that you have not, and I recommend that you do so if you care about the topic. Again, parsing is a tricky topic and there are entire programming languages only for writing parsers. |
09-16-2012, 06:54 AM | #20 |
FFR Simfile Author
|
Re: Continuin to fail at Java
If the algorithm is logically expressed by 'do this loop, then do this loop', why not have two loops?
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|