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

Java tokenizer question

post #1 of 7
Thread Starter 
while (in.hasNext())
{
String line = in.nextLine();
System.out.println(line);

{
StringTokenizer token = new StringTokenizer(line, ",");
System.out.println("Tokens follow");
while (token.hasMoreTokens())
{
System.out.println(token.nextToken());
if (token == ">")
{
System.out.println("");
}
}
}
}


Why can't I compare a token to a string? getting an incompatible types error on if (token ==">").

Thanks.
post #2 of 7
In the code above token is a 'StringTokenizer' object.

When you called the 'nextToken' method it removes the next token from the StringTokenizer object. Returning a string.

That is how your
System.out.println(token.nextToken());
line works.

The problem is if you wish to do a compare on the token you just retrieved, you need to store it somewhere to use it first. As once you remove it you can't remove it again, if you get what I mean.

Let me know if you have more questions.


edit:

Also if token was a string you would not be able to compare it with a boolean equivalency operator.
Edited by rush2049 - 2/6/12 at 4:26pm
 
Tragbar
(14 items)
 
 
CPUMotherboardGraphicsRAM
Ryzen 1800X Asus Crosshair VI Hero Nvidia Geforce Titan X F4-3600C15D-16GTZ 
Hard DriveHard DriveHard DriveHard Drive
Combined: 6.13 TB of space Samsung SSD 960 EVO 500GB Crucial_CT1050MX300SSD1 M4-CT128M4SSD2 
CoolingCoolingCoolingCooling
EK-Supremacy EVO White Edition PrimoChill PrimoFlex Advanced LRT EK-RES X4 250 (R2.0) Reservoir EK-CoolStream XE 360 
CoolingCoolingCoolingOS
EK-Vardar F4-120ER (2200rpm)  EK-Ekoolant EVO Liquid Coolant EK-XTOP Revo D5 PWM Pump Windows 10 Pro 
MonitorKeyboardPowerCase
Asus VG278H Ducky YOTG Keyboard Corsair AX1200 Corsair Obsidian 800D 
MouseAudioAudioAudio
Logitech G700s Schiit Gungnir Multibit Schiit Mjolnir 2 Schiit LISST 
Audio
Audeze LCD 2 - pre fazor 
CPUMotherboardGraphicsRAM
intel i7 4770K ASRock Z87E-ITX Nvidia Geforce GTX Titan X GSkill F3-2400C10D-16GTX R 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 512GB Silverstone 12.7mm Slot Load Blu-Ray Burner Noctua NH-L12 Windows 8 Pro 
MonitorCaseAudioAudio
LG PA75U Slim LED Projector Silverstone SG08 Schiit Bifrost Schiit Asgard 2 
AudioOther
AKG Q701 Headphones Pelican 1510 Green Case  
  hide details  
Reply
 
Tragbar
(14 items)
 
 
CPUMotherboardGraphicsRAM
Ryzen 1800X Asus Crosshair VI Hero Nvidia Geforce Titan X F4-3600C15D-16GTZ 
Hard DriveHard DriveHard DriveHard Drive
Combined: 6.13 TB of space Samsung SSD 960 EVO 500GB Crucial_CT1050MX300SSD1 M4-CT128M4SSD2 
CoolingCoolingCoolingCooling
EK-Supremacy EVO White Edition PrimoChill PrimoFlex Advanced LRT EK-RES X4 250 (R2.0) Reservoir EK-CoolStream XE 360 
CoolingCoolingCoolingOS
EK-Vardar F4-120ER (2200rpm)  EK-Ekoolant EVO Liquid Coolant EK-XTOP Revo D5 PWM Pump Windows 10 Pro 
MonitorKeyboardPowerCase
Asus VG278H Ducky YOTG Keyboard Corsair AX1200 Corsair Obsidian 800D 
MouseAudioAudioAudio
Logitech G700s Schiit Gungnir Multibit Schiit Mjolnir 2 Schiit LISST 
Audio
Audeze LCD 2 - pre fazor 
CPUMotherboardGraphicsRAM
intel i7 4770K ASRock Z87E-ITX Nvidia Geforce GTX Titan X GSkill F3-2400C10D-16GTX R 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 512GB Silverstone 12.7mm Slot Load Blu-Ray Burner Noctua NH-L12 Windows 8 Pro 
MonitorCaseAudioAudio
LG PA75U Slim LED Projector Silverstone SG08 Schiit Bifrost Schiit Asgard 2 
AudioOther
AKG Q701 Headphones Pelican 1510 Green Case  
  hide details  
Reply
post #3 of 7
Quote:
Originally Posted by ElectricSauce View Post

while (in.hasNext())
{
String line = in.nextLine();
System.out.println(line);
{
StringTokenizer token = new StringTokenizer(line, ",");
System.out.println("Tokens follow");
while (token.hasMoreTokens())
{
System.out.println(token.nextToken());
if (token == ">")
{
System.out.println("");
}
}
}
}
Why can't I compare a token to a string? getting an incompatible types error on if (token ==">").
Thanks.

your code compares a string tokenizer object to a string. that is what produces the incompatible type error. try saving the nextToken, and then compare it to ">".
Code:
String temp = token.nextToken();
System.out.println(temp);
if (temp.equals(">"))

Edited by travesty - 2/6/12 at 4:30pm
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 #4 of 7
Here is your code cleaned up and fixed to the best of my understanding of what you wanted to accomplish.
Code:
while (in.hasNext()){
        String line = in.nextLine();
        System.out.println(line);//debug statement

        StringTokenizer token = new StringTokenizer(line, ",");
        System.out.println("Tokens follow");//debug statement
        while (token.hasMoreTokens()){
                String temp = token.nextToken();
                System.out.println("\n" + temp);//debug statement
                if (token.equals(">"))
                        System.out.println("This token contained a '>'");//debug statement
        }
}
 
Tragbar
(14 items)
 
 
CPUMotherboardGraphicsRAM
Ryzen 1800X Asus Crosshair VI Hero Nvidia Geforce Titan X F4-3600C15D-16GTZ 
Hard DriveHard DriveHard DriveHard Drive
Combined: 6.13 TB of space Samsung SSD 960 EVO 500GB Crucial_CT1050MX300SSD1 M4-CT128M4SSD2 
CoolingCoolingCoolingCooling
EK-Supremacy EVO White Edition PrimoChill PrimoFlex Advanced LRT EK-RES X4 250 (R2.0) Reservoir EK-CoolStream XE 360 
CoolingCoolingCoolingOS
EK-Vardar F4-120ER (2200rpm)  EK-Ekoolant EVO Liquid Coolant EK-XTOP Revo D5 PWM Pump Windows 10 Pro 
MonitorKeyboardPowerCase
Asus VG278H Ducky YOTG Keyboard Corsair AX1200 Corsair Obsidian 800D 
MouseAudioAudioAudio
Logitech G700s Schiit Gungnir Multibit Schiit Mjolnir 2 Schiit LISST 
Audio
Audeze LCD 2 - pre fazor 
CPUMotherboardGraphicsRAM
intel i7 4770K ASRock Z87E-ITX Nvidia Geforce GTX Titan X GSkill F3-2400C10D-16GTX R 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 512GB Silverstone 12.7mm Slot Load Blu-Ray Burner Noctua NH-L12 Windows 8 Pro 
MonitorCaseAudioAudio
LG PA75U Slim LED Projector Silverstone SG08 Schiit Bifrost Schiit Asgard 2 
AudioOther
AKG Q701 Headphones Pelican 1510 Green Case  
  hide details  
Reply
 
Tragbar
(14 items)
 
 
CPUMotherboardGraphicsRAM
Ryzen 1800X Asus Crosshair VI Hero Nvidia Geforce Titan X F4-3600C15D-16GTZ 
Hard DriveHard DriveHard DriveHard Drive
Combined: 6.13 TB of space Samsung SSD 960 EVO 500GB Crucial_CT1050MX300SSD1 M4-CT128M4SSD2 
CoolingCoolingCoolingCooling
EK-Supremacy EVO White Edition PrimoChill PrimoFlex Advanced LRT EK-RES X4 250 (R2.0) Reservoir EK-CoolStream XE 360 
CoolingCoolingCoolingOS
EK-Vardar F4-120ER (2200rpm)  EK-Ekoolant EVO Liquid Coolant EK-XTOP Revo D5 PWM Pump Windows 10 Pro 
MonitorKeyboardPowerCase
Asus VG278H Ducky YOTG Keyboard Corsair AX1200 Corsair Obsidian 800D 
MouseAudioAudioAudio
Logitech G700s Schiit Gungnir Multibit Schiit Mjolnir 2 Schiit LISST 
Audio
Audeze LCD 2 - pre fazor 
CPUMotherboardGraphicsRAM
intel i7 4770K ASRock Z87E-ITX Nvidia Geforce GTX Titan X GSkill F3-2400C10D-16GTX R 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 512GB Silverstone 12.7mm Slot Load Blu-Ray Burner Noctua NH-L12 Windows 8 Pro 
MonitorCaseAudioAudio
LG PA75U Slim LED Projector Silverstone SG08 Schiit Bifrost Schiit Asgard 2 
AudioOther
AKG Q701 Headphones Pelican 1510 Green Case  
  hide details  
Reply
post #5 of 7
Quote:
Originally Posted by travesty View Post


your code compares a string tokenizer object to a string. that is what produces the incompatible type error. try saving the nextToken, and then compare it to ">".
Code:
String temp = token.nextToken();
System.out.println(temp);
if (temp == ">")

You cannot use == to compare strings unless you meant to test the object's reference equality.
 
Tragbar
(14 items)
 
 
CPUMotherboardGraphicsRAM
Ryzen 1800X Asus Crosshair VI Hero Nvidia Geforce Titan X F4-3600C15D-16GTZ 
Hard DriveHard DriveHard DriveHard Drive
Combined: 6.13 TB of space Samsung SSD 960 EVO 500GB Crucial_CT1050MX300SSD1 M4-CT128M4SSD2 
CoolingCoolingCoolingCooling
EK-Supremacy EVO White Edition PrimoChill PrimoFlex Advanced LRT EK-RES X4 250 (R2.0) Reservoir EK-CoolStream XE 360 
CoolingCoolingCoolingOS
EK-Vardar F4-120ER (2200rpm)  EK-Ekoolant EVO Liquid Coolant EK-XTOP Revo D5 PWM Pump Windows 10 Pro 
MonitorKeyboardPowerCase
Asus VG278H Ducky YOTG Keyboard Corsair AX1200 Corsair Obsidian 800D 
MouseAudioAudioAudio
Logitech G700s Schiit Gungnir Multibit Schiit Mjolnir 2 Schiit LISST 
Audio
Audeze LCD 2 - pre fazor 
CPUMotherboardGraphicsRAM
intel i7 4770K ASRock Z87E-ITX Nvidia Geforce GTX Titan X GSkill F3-2400C10D-16GTX R 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 512GB Silverstone 12.7mm Slot Load Blu-Ray Burner Noctua NH-L12 Windows 8 Pro 
MonitorCaseAudioAudio
LG PA75U Slim LED Projector Silverstone SG08 Schiit Bifrost Schiit Asgard 2 
AudioOther
AKG Q701 Headphones Pelican 1510 Green Case  
  hide details  
Reply
 
Tragbar
(14 items)
 
 
CPUMotherboardGraphicsRAM
Ryzen 1800X Asus Crosshair VI Hero Nvidia Geforce Titan X F4-3600C15D-16GTZ 
Hard DriveHard DriveHard DriveHard Drive
Combined: 6.13 TB of space Samsung SSD 960 EVO 500GB Crucial_CT1050MX300SSD1 M4-CT128M4SSD2 
CoolingCoolingCoolingCooling
EK-Supremacy EVO White Edition PrimoChill PrimoFlex Advanced LRT EK-RES X4 250 (R2.0) Reservoir EK-CoolStream XE 360 
CoolingCoolingCoolingOS
EK-Vardar F4-120ER (2200rpm)  EK-Ekoolant EVO Liquid Coolant EK-XTOP Revo D5 PWM Pump Windows 10 Pro 
MonitorKeyboardPowerCase
Asus VG278H Ducky YOTG Keyboard Corsair AX1200 Corsair Obsidian 800D 
MouseAudioAudioAudio
Logitech G700s Schiit Gungnir Multibit Schiit Mjolnir 2 Schiit LISST 
Audio
Audeze LCD 2 - pre fazor 
CPUMotherboardGraphicsRAM
intel i7 4770K ASRock Z87E-ITX Nvidia Geforce GTX Titan X GSkill F3-2400C10D-16GTX R 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 512GB Silverstone 12.7mm Slot Load Blu-Ray Burner Noctua NH-L12 Windows 8 Pro 
MonitorCaseAudioAudio
LG PA75U Slim LED Projector Silverstone SG08 Schiit Bifrost Schiit Asgard 2 
AudioOther
AKG Q701 Headphones Pelican 1510 Green Case  
  hide details  
Reply
post #6 of 7
Quote:
Originally Posted by rush2049 View Post

You cannot use == to compare strings unless you meant to test the object's reference equality.

thanks, already fixed. i haven't had to code in java in a while....starting to forget details like that.
Edited by travesty - 2/6/12 at 4:39pm
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 #7 of 7
Thread Starter 
It makes sense now. What I was trying to do was parse a web page, but I could only get a single long string out of the whole thing, which is why I wanted to compare to a ">". I found out that the String class has a split method that worked out perfectly for me:

String addressToBeParsed = "http://www.google.com/ig/api?stock=AAPL&stock=GOOG";
URL locator = new URL(addressToBeParsed);
Scanner in = new Scanner(locator.openStream());

while (in.hasNext())
{
String line = in.nextLine();
System.out.println(line);


String delims = "[><]";
String[] tokens = line.split(delims);

for (int i = 0; i < tokens.length; i++)
System.out.println(tokens);

Thanks for they help everybody.
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 tokenizer question