New Posts  All Forums:Forum Nav:

[Java] if statement issues?

post #1 of 10
Thread Starter 
Hey guys another quick question. sorry to spam the forum but it's this last bit of code I just can't get to work.

Basically here's the code

Code:

String subjectFromField = subjectField.getText();

if (subjectFromField == "")
{
String subjectFromField = MessageData.DEFAULT_SUBJECT;
}
I'm pretty sure I'm doing something wrong but I can't see it out of tiredness and stupidty. Basically I want to grab some info from subjectField and name it as a string called subjectFromField. But if this value from grabbed from subjectfield is empty it'll call up a string from another class (MessageData) and set that.

So what am I doing wrong? I keep getting the same error message that subjectFromField is already defined. I don't want to rename it as then it'd only send one value...

Help!!
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 10
You already defined subjectFromField.

When you mention it within the if statement, you do not need to redefine it as a string. Simply assign it's value.

Code:
String subjectFromField = subjectField.getText();

if (subjectFromField == "")
{
subjectFromField = MessageData.DEFAULT_SUBJECT;
}
    
CPUMotherboardGraphicsRAM
Phenom II X3 720 @ 3.5 (1.39v) Gigabyte GA-MA790XT-UD4P ASUS 5850 @ 880/1180 (1.118v) 4gb 1333mhz G.Skill DDR3 
Hard DriveHard DriveHard DriveHard Drive
64gb Crucial M4 SSD 3x 250gb WD2500JD - RAID0 1tb WD1001FALS 1tb WD10EARS 
Optical DriveCoolingOSMonitor
Pioneer DLDVD/CDRW Arctic Freezer Pro 64 Win 7 64bit Ultimate E2305 LG 23" LED 
KeyboardPowerCaseMouse
Filco Majestouch II Ninja Tenkeyless Fortron BlueStorm II 500w CM 690II Advanced Razer DeathAdder 
Mouse PadAudioAudio
generic Echo Miamidi PCI M-Audio BX5a Studio Monitors 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom II X3 720 @ 3.5 (1.39v) Gigabyte GA-MA790XT-UD4P ASUS 5850 @ 880/1180 (1.118v) 4gb 1333mhz G.Skill DDR3 
Hard DriveHard DriveHard DriveHard Drive
64gb Crucial M4 SSD 3x 250gb WD2500JD - RAID0 1tb WD1001FALS 1tb WD10EARS 
Optical DriveCoolingOSMonitor
Pioneer DLDVD/CDRW Arctic Freezer Pro 64 Win 7 64bit Ultimate E2305 LG 23" LED 
KeyboardPowerCaseMouse
Filco Majestouch II Ninja Tenkeyless Fortron BlueStorm II 500w CM 690II Advanced Razer DeathAdder 
Mouse PadAudioAudio
generic Echo Miamidi PCI M-Audio BX5a Studio Monitors 
  hide details  
Reply
post #3 of 10
Thread Starter 
Hmmm that worked the only problem now that when I run the program and leave those fields empty it just passes on an empty value :/

If this helps here's the full code

Code:
String id = messageID.getText();
System.out.println("the id is "+ Integer.parseInt(messageID.getText()));


String subjectFromField = subjectField.getText();

if (subjectFromField == null)
{
subjectFromField = MessageData.DEFAULT_SUBJECT;
}

String recipientEmail = recipientField.getText();
String brandNewMessage = textArea.getText();

String senderEmailField = senderField.getText();

if (senderEmailField == null)
{
senderEmailField = MessageData.DEFAULT_EMAIL;
}

MessageData.createMessage(id, recipientEmail, subjectFromField, brandNewMessage, senderEmailField);
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #4 of 10
Try replacing these two if statements:
Code:
if (subjectFromField == null)
{
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (senderEmailField == null)
{
     senderEmailField = MessageData.DEFAULT_EMAIL;
}
With something like this
Code:
if (subjectFromField.length() == 0)
{
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (senderEmailField.length() == 0)
{
     senderEmailField = MessageData.DEFAULT_EMAIL;
}
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #5 of 10
Or...

Code:
if (StringUtils.isEmpty(subjectFromField))
{
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (StringUtils.isEmpty(senderEmailField))
{
     senderEmailField = MessageData.DEFAULT_EMAIL;
}
To make it even more robust, as someone could enter white spaces.

Code:
if (StringUtils.isEmpty(subjectFromField.trim()))
{
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (StringUtils.isEmpty(senderEmailField.trim()))
{
     senderEmailField = MessageData.DEFAULT_EMAIL;
}
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #6 of 10
Thread Starter 
Ahh that worked nicely Cheers for that! Rep+
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #7 of 10
Thread Starter 
Quote:
Originally Posted by mykah89 View Post
Or...

Code:
if (StringUtils.isEmpty(subjectFromField))
{
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (StringUtils.isEmpty(senderEmailField))
{
     senderEmailField = MessageData.DEFAULT_EMAIL;
}
To make it even more robust, as someone could enter white spaces.

Code:
if (StringUtils.isEmpty(subjectFromField.trim()))
{
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (StringUtils.isEmpty(senderEmailField.trim()))
{
     senderEmailField = MessageData.DEFAULT_EMAIL;
}

Ahh the latter is what I was looking for. No pesky empty spaces! Although StringUtils?? what's that?
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #8 of 10
Thread Starter 
I tried using common sense and changing StringUtils to my strings name.... but I get this error message.
Code:
---------- JavaC ----------
NewMessages.java:86: isEmpty() in java.lang.String cannot be applied to (java.lang.String)
if (subjectFromField.isEmpty(subjectFromField.trim()))
                    ^
NewMessages.java:96: isEmpty() in java.lang.String cannot be applied to (java.lang.String)
if (senderEmailField.isEmpty(senderEmailField.trim()))
                    ^
2 errors

Output completed (1 sec consumed) - Normal Termination
Help!!!
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #9 of 10
StringUtils is what allows you to use the 'isEmpty' method. Basically it is a utility class that exists as somewhat of a helper function. isEmpty is one of it's useful functions.

So you should not rename it, as subjectFromField cannot perform an 'isEmpty' check. Moreover, the subjectFromField.trim() method that has already been invoked is what's being checked as empty or not.
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
post #10 of 10
Yes do not rename the StringUtils call. It's also good practice to use .equals() on non-primitive data types in Java, just as a heads up. In my mind, to keep your dependence on packages low, I would:

Code:
if (subjectFromField.trim().equals("")){
     subjectFromField = MessageData.DEFAULT_SUBJECT;
}
if (senderEmailField.trim().equals("")){
     senderEmailField = MessageData.DEFAULT_EMAIL;
}

Edited by LetThereBeDwight - 3/14/11 at 11:35pm
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming