Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help using String Tokenizer in Java
New Posts  All Forums:Forum Nav:

Help using String Tokenizer in Java

post #1 of 9
Thread Starter 
Hey guys, I just had a quick question. I am required to use String Tokenizer to read in the values of a file. The format of the .txt file is a state, then it's population. Like this:

California 55000000
Texas 28000000
Nevada 6500000
etc....

I read the numbers in fine, but I need to get it to display with commas. Like this:

California 55,000,000
etc.....

I am trying to use NumberFormat.getInstance() to accomplish this, but I keep getting this error message:

"Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number"

This is the last thing I have to do on this project but it's proving to be quite the challenge. Any help would be greatly appreciated. Thanks!
post #2 of 9
idk why NumberFormat.getInstance() would give you that error.

i assume you tried to do something like this?
Code:
myString = NumberFormat.getInstance().format(myNumber);
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
post #3 of 9
Thread Starter 
Yes that's almost the exact line of code that I have. I have this:

Code:
String temp= nf.getInstance().format(newPop);
post #4 of 9
Did you convert newPop to a number type? Either double or int?
post #5 of 9
yeah, as long as newPop is a double, it should work.
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
post #6 of 9
Thread Starter 
This is the code that I have now. Temp is a String assigned to read in the file. (I used Buffered reader.)

Code:
FileReader fr2 = new FileReader("Link.States.Trans.txt");
        BufferedReader br2 = new BufferedReader(fr2);
        String temp = br2.readLine();

            StringTokenizer st = new StringTokenizer(temp);
            while (st.hasMoreTokens())
            {
     
                stName = st.nextToken();
                newPop = st.nextToken();
                int temp1 = Integer.parseInt(newPop);
                NumberFormat.getInstance().format(temp1);
            }
that fixed that problem I had earlier(thanks so much, +rep to both of you), but the outputs now look like this....

California 5.5E7
Texas 2.8E7
etc....

any idea as to what the problem is now??
post #7 of 9
^That just means it's outputting them to the power of x. I don't know how to fix that though...
Main Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 DO 4.0ghz HT Rampage II Extreme 7870 Gigabyte OC 6gb DDR3 Corsair Dominator 
Hard DriveOptical DriveOSMonitor
SSD Intel 160gb gen 2! - 1TB F3 Samsung 22x Lightscribe Arch Linux - Cinnamon DE 2048X1152 SyncMaster 2343 
KeyboardPowerCaseMouse
Das Ultimate S Corsair 750w ANTEC 900 V2! Logitech G9 - Best Mouse Ever! 
  hide details  
Reply
Main Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 DO 4.0ghz HT Rampage II Extreme 7870 Gigabyte OC 6gb DDR3 Corsair Dominator 
Hard DriveOptical DriveOSMonitor
SSD Intel 160gb gen 2! - 1TB F3 Samsung 22x Lightscribe Arch Linux - Cinnamon DE 2048X1152 SyncMaster 2343 
KeyboardPowerCaseMouse
Das Ultimate S Corsair 750w ANTEC 900 V2! Logitech G9 - Best Mouse Ever! 
  hide details  
Reply
post #8 of 9
Thread Starter 
Yeah. I really don't see why it isn't working.... I've never had a problem with NumberFormat at all before. Although I guess I've never used StringTokenizer to read in files.
post #9 of 9
Thread Starter 
I got it! Of course it was a dumb mistake. I completely forgot to store it into a String value after using parseInt. Here's the code that made it work, if anyone was wondering...


Code:
                stName = st.nextToken();
                newPop = st.nextToken();
                
                
                int temp1 = Integer.parseInt(newPop);
                String temp2= nf.format(temp1);
Thanks again everyone for your help. Sometimes it helps just to talk through it with someone.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help using String Tokenizer in Java