Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java word scrambling question.
New Posts  All Forums:Forum Nav:

Java word scrambling question.

post #1 of 5
Thread Starter 
Pretty much, reads the input from textBox1. Scrambles the words. Prints to textBox2.
However I keep on getting null pointer errors.
If anyone of you have worked with StringTokenizer, and can help I would greatly appreciate it.

Code:
    
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        
        String text = jTextArea1.getText();
        StringTokenizer st = new StringTokenizer(text);
        String[] word = new String[100001];
        int h = 0;
        
        while (st.hasMoreTokens())  {
            for (int j = 0; j < word.length; j++) {
                word[j] = (st.nextToken());
                //jTextArea2.append(word[j]);
            }
        }
       
        while (h < 100) {
        int j = 0;    
        ArrayList<Character> chars = new ArrayList<Character>(word[j].length());
            for ( char c : word[j].toCharArray() ) {
                chars.add(c);
                j++;
            }
        Collections.shuffle(chars);
        
        char[] shuffled = new char[chars.size()];
           
            for ( int i = 0; i < shuffled.length; i++ ) {
                shuffled[i] = chars.get(i);
            }
        String shuffledWord = new String(shuffled);
        
        jTextArea2.append(shuffledWord + "\
");
        h++;
        }
    }
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
post #2 of 5
You need to check if the "HAS" a next rather than trying to return it. But, I don't really understand why you are nesting loops like that.

Code:
while (st.hasMoreTokens())  {
            for (int j = 0; j < word.length; j++) {
                word[j] = (st.nextToken()); << this goes out of bounds.
                //jTextArea2.append(word[j]);
            }
        }
I sure there is a better way to scramble but I will fix your code for you.

It looks like you are trying to brake it down in to chars too, I don't understand why you are wanting to do that.

Well here is my solution

Code:
public static void main(String args[])
{
                                
        
        String text = "My name is Andrew McHugh I have fixed youre code";
        String res = "";

        ArrayList<String> word= new ArrayList<String>() ;
        
        StringTokenizer st = new StringTokenizer(text);
        
        while (st.hasMoreTokens())          
        word.add (st.nextToken());
        
        Collections.shuffle(word);
   
        for ( int i = 0; i < word.size(); i++ ) {
        res += (word.get(i) + "\
");
            }
        
        System.out.println(res);
    }
Quote:
Originally Posted by Console output from program
McHugh
I
Andrew
name
is
youre
My
have
code
fixed
So the fix for you're code will look like this.

Code:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    
    
String text = jTextArea1.getText();
    StringTokenizer st = new StringTokenizer(text);

    ArrayList<String> word= new ArrayList<String>() ;

    while (st.hasMoreTokens())          
    word.add (st.nextToken());
   
    
    Collections.shuffle(word);
    
    
    for ( int i = 0; i < word.size(); i++ ) {
    jTextArea2.append(word.get(i) + "\
");
        }


    }
}

Edited by andrewmchugh - 10/8/11 at 11:44am
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #3 of 5
Thread Starter 
+1

I went back through it and removed string tokenizer because it was legacy.

What I have now, works to scramble the word, and print each word. However now I am trying to leave the letter in the first position the same and the letter in the last position the same. But alas once again, I am having issues because substring, doesn't seem to work with the character array.

Code:
   
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        String[] result = jTextArea1.getText().split("\\\\s");
        for ( int i = 0; i < result.length; i++) {
            ArrayList<Character> chars = new ArrayList<Character>(result[i].length());
                for ( char c : result[i].toCharArray() ) {
                    chars.add(c);
                }
            Collections.shuffle(chars.substring(1, result[i].length()-1));      <---Problem
            char[] shuffled = new char[chars.size()];
                for ( int j = 0; j < shuffled.length; j++ ) {
                    shuffled[j] = chars.get(j);
                }
            String shuffledWord = new String(shuffled);
            jTextArea2.append(shuffledWord + " ");
        }
    }
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
post #4 of 5
So If the following String was typed how do you want it to look

"My name is Andrew"
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #5 of 5
Thread Starter 
Quote:
Originally Posted by andrewmchugh View Post
So If the following String was typed how do you want it to look

"My name is Andrew"
My nmae is aenrdw

Since the first and last letters stay the same, 2 letter words stay the same.
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java word scrambling question.