Continuin to fail at Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SKG_Scintill
    Spun a twirly fruitcake,
    FFR Simfile Author
    • Feb 2009
    • 3875

    #1

    Continuin 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):
    Code:
    package start;
    
    import javax.swing.JFrame;
    import view.GUI;
    
    public class Main {
    	public static void main(String[] args) {
    		GUI a = new GUI();
    		a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		a.setVisible(true);
    	}
    }


    GUI class (works properly):
    Code:
    package view;
    
    import java.awt.*;
    import javax.swing.*;
    import controller.HiraganaController;
    
    public class GUI extends JFrame{
    	private static final long serialVersionUID = 1L;
    	private HiraganaController KeyCon = new HiraganaController(this);
    	
    	public GUI(){
    		Container window = getContentPane();
    		setTitle("Hiragana Notepad");
    		setSize(640,480);
    		
    		JPanel mainpanel = new JPanel();
    		mainpanel.setLayout(null);
    		mainpanel.setBackground(Color.DARK_GRAY);
    		mainpanel.setBounds(0,0,640,480);
    		window.add(mainpanel);
    		
    		JTextArea area = new JTextArea();
    		area.setBounds(5,5,614,432);
    		area.setLineWrap(true);
    		area.addKeyListener(KeyCon);
    		mainpanel.add(area);
    	}
    }


    HiraganaController class (no object is made, program doesn't even get to the switch):
    Code:
    package controller;
    
    import java.awt.event.*;
    import view.GUI;
    import model.HiraganaConfig;
    
    public class HiraganaController implements KeyListener{
    	private GUI gui;
    	String prev;
    	
    	public HiraganaController(GUI gui){
    		this.gui = gui;
    	}
    	
    	public void keyPressed(KeyEvent e){}
    	public void keyReleased(KeyEvent e){}
    	public void keyTyped(KeyEvent e){
    		int key = e.getKeyCode();
    		switch(key){
    		case KeyEvent.VK_A:
    			System.out.println("Works");
    		break;
    		case KeyEvent.VK_E:
    			
    		break;
    		case KeyEvent.VK_I:
    			
    		break;
    		case KeyEvent.VK_O:
    			
    		break;
    		case KeyEvent.VK_U:
    			
    		break;
    		}
    	}
    }


    HiraganaConfig class (Just a husk of code, will come later):
    Code:
    package model;
    
    import view.GUI;
    
    public class HiraganaConfig{
    	private GUI gui = new GUI();
    	String syl;
    	
    	public String aConfig(String prev){
    		return syl;
    	}
    	public String eConfig(String prev){
    		return syl;
    	}
    	public String iConfig(String prev){
    		return syl;
    	}
    	public String oConfig(String prev){
    		return syl;
    	}
    	public String uConfig(String prev){
    		return syl;
    	}
    }


    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?
    Last edited by SKG_Scintill; 09-8-2012, 03:40 PM.





    Originally posted by bluguerilla
    So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
    ___
    . RHYTHMS PR LAYERING
    . ZOMG I HAD TO QUIT OUT TERRIBLE
    .
  • Patashu
    FFR Simfile Author
    FFR Simfile Author
    • Apr 2006
    • 8609

    #2
    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

    Comment

    • SKG_Scintill
      Spun a twirly fruitcake,
      FFR Simfile Author
      • Feb 2009
      • 3875

      #3
      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!





      Originally posted by bluguerilla
      So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
      ___
      . RHYTHMS PR LAYERING
      . ZOMG I HAD TO QUIT OUT TERRIBLE
      .

      Comment

      • SKG_Scintill
        Spun a twirly fruitcake,
        FFR Simfile Author
        • Feb 2009
        • 3875

        #4
        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;
        	}
        }
        Obviously this doesn't work, it tells me so:
        Cannot switch on a value of type int[]. Only convertible int values or enum constants are permitted
        Is it possible some other way, though?
        Last edited by SKG_Scintill; 09-9-2012, 05:22 AM.





        Originally posted by bluguerilla
        So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
        ___
        . RHYTHMS PR LAYERING
        . ZOMG I HAD TO QUIT OUT TERRIBLE
        .

        Comment

        • Patashu
          FFR Simfile Author
          FFR Simfile Author
          • Apr 2006
          • 8609

          #5
          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

          Comment

          • SKG_Scintill
            Spun a twirly fruitcake,
            FFR Simfile Author
            • Feb 2009
            • 3875

            #6
            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){}
            }
            When I type "ba", it gives me a NullPointerException
            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);
            But to no avail; still a NullPointerException.

            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?
            Last edited by SKG_Scintill; 09-10-2012, 05:51 AM.





            Originally posted by bluguerilla
            So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
            ___
            . RHYTHMS PR LAYERING
            . ZOMG I HAD TO QUIT OUT TERRIBLE
            .

            Comment

            • Patashu
              FFR Simfile Author
              FFR Simfile Author
              • Apr 2006
              • 8609

              #7
              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

              Comment

              • SKG_Scintill
                Spun a twirly fruitcake,
                FFR Simfile Author
                • Feb 2009
                • 3875

                #8
                Re: Continuin to fail at Java

                Thankfully it's all small problems :P





                Originally posted by bluguerilla
                So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                ___
                . RHYTHMS PR LAYERING
                . ZOMG I HAD TO QUIT OUT TERRIBLE
                .

                Comment

                • SKG_Scintill
                  Spun a twirly fruitcake,
                  FFR Simfile Author
                  • Feb 2009
                  • 3875

                  #9
                  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:
                  Code:
                  package view;
                  
                  import java.awt.*;
                  import javax.swing.*;
                  import controller.HiraganaController;
                  
                  public class GUI extends JFrame{
                  	private static final long serialVersionUID = 1L;
                  	private HiraganaController KeyCon = new HiraganaController(this);
                  	public JTextArea hirarea = new JTextArea();
                  	
                  	public GUI(){
                  		Container window = getContentPane();
                  		setTitle("Hiragana Notepad");
                  		setSize(640,480);
                  		
                  		JPanel mainpanel = new JPanel();
                  		mainpanel.setLayout(null);
                  		mainpanel.setBounds(0,0,640,480);
                  		window.add(mainpanel);
                  		
                  		JTextArea area = new JTextArea();
                  		area.setBounds(5,5,614,300);
                  		area.setBorder(BorderFactory.createMatteBorder(3,3,3,3,Color.DARK_GRAY));
                  		area.setLineWrap(true);
                  		area.addKeyListener(KeyCon);
                  		mainpanel.add(area);
                  		
                  		hirarea.setBounds(5,311,614,125);
                  		hirarea.setBorder(BorderFactory.createMatteBorder(3,3,3,3,Color.DARK_GRAY));
                  		hirarea.setLineWrap(true);
                  		mainpanel.add(hirarea);
                  	}
                  }


                  HiraganaConfig code:
                  Code:
                  package model;
                  
                  import view.GUI;
                  
                  public class HiraganaConfig{
                  	private GUI gui = new GUI();
                  	
                  	public void hirConfig(int key, int prev){
                  		switch(key){
                  		case 65:
                  			switch(prev){
                  			case 1:
                  				gui.hirarea.setText("I ");
                  			break;
                  			case 66:
                  				gui.hirarea.setText("Don't ");
                  			break;
                  			}
                  		break;
                  		case 69:
                  			
                  		break;
                  		case 73:
                  			
                  		break;
                  		case 79:
                  			
                  		break;
                  		case 85:
                  			
                  		break;
                  		}
                  	}
                  }


                  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.





                  Originally posted by bluguerilla
                  So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                  ___
                  . RHYTHMS PR LAYERING
                  . ZOMG I HAD TO QUIT OUT TERRIBLE
                  .

                  Comment

                  • SKG_Scintill
                    Spun a twirly fruitcake,
                    FFR Simfile Author
                    • Feb 2009
                    • 3875

                    #10
                    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()





                    Originally posted by bluguerilla
                    So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                    ___
                    . RHYTHMS PR LAYERING
                    . ZOMG I HAD TO QUIT OUT TERRIBLE
                    .

                    Comment

                    • SKG_Scintill
                      Spun a twirly fruitcake,
                      FFR Simfile Author
                      • Feb 2009
                      • 3875

                      #11
                      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:
                      Code:
                      package view;
                      
                      import java.awt.*;
                      import javax.swing.*;
                      import controller.HiraganaController;
                      
                      public class GUI extends JFrame{
                      	private static final long serialVersionUID = 1L;
                      	private HiraganaController KeyCon = new HiraganaController(this);
                      	public static JTextArea area = new JTextArea();
                      	public static JTextArea hirarea = new JTextArea();
                      	
                      	public GUI(){
                      		Container window = getContentPane();
                      		setTitle("Hiragana Notepad");
                      		setSize(640,480);
                      		
                      		JPanel mainpanel = new JPanel();
                      		mainpanel.setLayout(null);
                      		mainpanel.setBackground(Color.DARK_GRAY);
                      		mainpanel.setBounds(0,0,640,480);
                      		window.add(mainpanel);
                      		
                      		area.setBounds(5,5,614,300);
                      		area.setLineWrap(true);
                      		area.addKeyListener(KeyCon);
                      		mainpanel.add(area);
                      		
                      		hirarea.setBounds(5,311,614,124);
                      		hirarea.setLineWrap(true);
                      		mainpanel.add(hirarea);
                      	}
                      }

                      Control:
                      Code:
                      package controller;
                      
                      import java.awt.event.*;
                      import model.SymConfig;
                      import view.GUI;
                      
                      public class HiraganaController implements KeyListener{
                      	public HiraganaController(GUI gui){}
                      	
                      	public void keyPressed(KeyEvent e){}
                      	public void keyReleased(KeyEvent e){
                      		SymConfig config = new SymConfig();
                      		GUI.hirarea.setText(GUI.area.getText());
                      		config.hirConfig();
                      	}
                      	public void keyTyped(KeyEvent e){}
                      }

                      Model:
                      Code:
                      package model;
                      
                      import view.GUI;
                      
                      public class SymConfig{
                      	public void hirConfig(){
                      		String input = GUI.area.getText().toLowerCase();
                      
                      //--	A
                      		input = input.replace("ba","ば");
                      		input = input.replace("da","だ");
                      		input = input.replace("ga","が");
                      		input = input.replace("ha","は");
                      		input = input.replace("ja","じゃ");
                      		input = input.replace("kya","きゃ");
                      		input = input.replace("ka","か");
                      		input = input.replace("ma","ま");
                      		input = input.replace("na","な");
                      		input = input.replace("pa","ぱ");
                      		input = input.replace("ra","ら");
                      		input = input.replace("sa","さ");
                      		input = input.replace("ta","た");
                      		input = input.replace("wa","わ");
                      		input = input.replace("ya","や");
                      		input = input.replace("za","ざ");
                      		input = input.replace("a","あ");
                      		
                      //--	E
                      		input = input.replace("be","べ");
                      		input = input.replace("de","で");
                      		input = input.replace("ge","げ");
                      		input = input.replace("he","へ");
                      		input = input.replace("ke","け");
                      		input = input.replace("me","め");
                      		input = input.replace("ne","ね");
                      		input = input.replace("pe","ぺ");
                      		input = input.replace("re","れ");
                      		input = input.replace("se","せ");
                      		input = input.replace("te","て");
                      		input = input.replace("ze","ぜ");
                      		input = input.replace("e","え");
                      		
                      //--	I
                      		input = input.replace("bi","び");
                      		input = input.replace("gi","ぎ");
                      		input = input.replace("chi","ち");
                      		input = input.replace("shi","し");
                      		input = input.replace("hi","ひ");
                      		input = input.replace("ji","じ");
                      		input = input.replace("ki","き");
                      		input = input.replace("mi","み");
                      		input = input.replace("ni","に");
                      		input = input.replace("pi","ぴ");
                      		input = input.replace("ri","り");
                      		input = input.replace("i","い");
                      		
                      //--	O
                      		input = input.replace("bo","ぼ");
                      		input = input.replace("do","ど");
                      		input = input.replace("go","ご");
                      		input = input.replace("ho","ほ");
                      		input = input.replace("jo","じょ");
                      		input = input.replace("ko","こ");
                      		input = input.replace("mo","も");
                      		input = input.replace("no","の");
                      		input = input.replace("po","ぽ");
                      		input = input.replace("ro","ろ");
                      		input = input.replace("so","そ");
                      		input = input.replace("to","と");
                      		input = input.replace("wo","を");
                      		input = input.replace("yo","よ");
                      		input = input.replace("zo","ぞ");
                      		input = input.replace("o","お");
                      		
                      //--	U
                      		input = input.replace("bu","ぶ");
                      		input = input.replace("fu","ふ");
                      		input = input.replace("gu","ぐ");
                      		input = input.replace("ju","じゅ");
                      		input = input.replace("ku","く");
                      		input = input.replace("mu","む");
                      		input = input.replace("nu","ぬ");
                      		input = input.replace("pu","ぷ");
                      		input = input.replace("ru","る");
                      		input = input.replace("tsu","つ");
                      		input = input.replace("su","す");
                      		input = input.replace("vu","ゔ");
                      		input = input.replace("yu","ゆ");
                      		input = input.replace("zu","ず");
                      		input = input.replace("u","う");
                      		
                      //--	N
                      		input = input.replace("n","ん");
                      		GUI.hirarea.setText(input);
                      	}
                      }


                      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?
                      Last edited by SKG_Scintill; 09-11-2012, 07:47 AM.





                      Originally posted by bluguerilla
                      So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                      ___
                      . RHYTHMS PR LAYERING
                      . ZOMG I HAD TO QUIT OUT TERRIBLE
                      .

                      Comment

                      • SKG_Scintill
                        Spun a twirly fruitcake,
                        FFR Simfile Author
                        • Feb 2009
                        • 3875

                        #12
                        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?





                        Originally posted by bluguerilla
                        So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                        ___
                        . RHYTHMS PR LAYERING
                        . ZOMG I HAD TO QUIT OUT TERRIBLE
                        .

                        Comment

                        • UserNameGoesHere
                          FFR Veteran
                          • May 2008
                          • 1114

                          #13
                          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.

                          Originally posted by Crashfan3
                          Man, what would we do without bored rednecks?

                          Comment

                          • SKG_Scintill
                            Spun a twirly fruitcake,
                            FFR Simfile Author
                            • Feb 2009
                            • 3875

                            #14
                            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));
                            	}
                            }
                            I tried adding another if-statement in the loop for the hiraganagaeshi, but they cancel eachother out; it's one or the other.
                            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) + "ゝ");
                            	}
                            }
                            Can you make it so both ifs work at the same time?
                            Last edited by SKG_Scintill; 09-13-2012, 09:55 AM.





                            Originally posted by bluguerilla
                            So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                            ___
                            . RHYTHMS PR LAYERING
                            . ZOMG I HAD TO QUIT OUT TERRIBLE
                            .

                            Comment

                            • Patashu
                              FFR Simfile Author
                              FFR Simfile Author
                              • Apr 2006
                              • 8609

                              #15
                              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

                              Comment

                              Working...