Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED] Getting random word to print out in an Array
New Posts  All Forums:Forum Nav:

[SOLVED] Getting random word to print out in an Array

post #1 of 2
Thread Starter 
I'm doing this for a class project, hang man, I successfully got it to choose a random word, but it doesn't give me a print out of the word, just the number. When I tell the code to write out the number of the word chosen, it always defaults to null.

Here's the code:

Code:
import java.util.Random;
public class Dictionary{

public static void main(String[] args){



//Declaring a variable
//Build an array of words
{String words[] = new String[23];
 
words[1] = "dictionary";
words[2] = "restaurant";
words[3] = "televiion";
words[4] = "responsible";
words[5] = "technology";
words[6] = "collection";
words[7] = "communicate";
words[8] = "automobile";
words[9] = "federation";
words[10] = "enterprise";
words[11] = "exaggerate";
words[12] = "cappuccino";
words[13] = "monkey";
words[14] = "computer";
words[15] = "information";
words[16] = "systems";
words[17] = "poker";
words[18] = "bottle";
words[19] = "administration";
words[20] = "encyclopedia";
words[21] = "textbook";
words[22] = "calculus";

for (int index = 0; index<words.length; index++)
    
System.out.println(words[index]);
}

//Return a randomly selected word
{
String words [] = new String[23];

Random getRandomWord = new Random();

int n = getRandomWord.nextInt(23);

System.out.println("Your random number is " + n);
System.out.println("Your random word is " + words[n]);
System.out.println("This word has " + words[n].length() + " characters");

for (int i = 0; i<words[n].length(); i++)
System.out.print("_ ");

    Random rnd= new Random();
    int randomWord=rnd.nextInt(22);
    System.out.println("Your word is   "+ words[randomWord]);
}
}
}

Here's the part i'm having issues with:
Code:
System.out.println("Your random number is " + n);
System.out.println("Your random word is " + words[n]);

When I try to grab the word, words[n], I get the output for the following line:
Code:
Your random word is null
What am I doing wrong?

Thanks!
Edited by manifest3r - 2/16/11 at 3:28pm
 
FreeNAS
(9 items)
 
 
CPUMotherboardRAMHard Drive
Xeon W3503 T3500 Motherboard - 09KPNV Kingston 8GB ECC KVR13E9/8I WD Green 2 TB WD20EARX /S300-wdidle 
Hard DriveCoolingOSPower
WD Green 1.5 TB WD15EARX /S300-wdidle ARCTIC Alpine 64 GT Rev. 2  FreeNAS 9.2 RAIDMAX Cobra 400W RX-400AF-B 
Case
T3500 
  hide details  
Reply
 
FreeNAS
(9 items)
 
 
CPUMotherboardRAMHard Drive
Xeon W3503 T3500 Motherboard - 09KPNV Kingston 8GB ECC KVR13E9/8I WD Green 2 TB WD20EARX /S300-wdidle 
Hard DriveCoolingOSPower
WD Green 1.5 TB WD15EARX /S300-wdidle ARCTIC Alpine 64 GT Rev. 2  FreeNAS 9.2 RAIDMAX Cobra 400W RX-400AF-B 
Case
T3500 
  hide details  
Reply
post #2 of 2
this first thing i noticed was words is size 23 however you never add anything to words[0]. the range of words is 0-22. so if the random number was ever 0 it would print null.

however the reason it always prints null is because you reassign words[] to a blank array and never initialize any of the indices.

Code:
String words [] = new String[23];
if you delete that line and add a value to words[0] you should be fine
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  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 › [SOLVED] Getting random word to print out in an Array