How do I check to see if a character is NOT contained in a String?

I am trying to create a program which will read in user inputted text. If the text contains an email address, then the program will output it to screen. However, I also need to put some sort of fall back in there, in case an email address is not contained in the string.
This is the part I am having problems with. If I enter an email address, it will output it to screen. Maybe someone can help me with a "no valid mail" message.
I decided to use the '@' character as a reference point, since it is a unique character and should not be used in normal sentances. Since an email address is a continous block of text with no spaces in it,I created 2 substrings - one from '@' going backwards towards the previous space, and one from '@' going forward to the next space.
First, I tried the following:
//if(input.indexOf('@') == -1){
     // System.out.println("-no valid email-");
The program ran and when I entered text containing an email adress, it displayed it on screen. If I didn't include one, it gave me an error: java.lang.StringIndexOutOfBoundsException: -1
I'm guessing this is happening because if there is no '@' symbol in the user input, and so there is an error with the indexOf('@') statement. Any way to get around this?
Then I tried the following, bearing in mind the variable length is defined as "input.length()".
for(int i = 0; i <= length; i++){
     if(input.charAt(i) = '@'){
          break;
     if(i == length){
     System.out.println("-no valid email-");
this gives me an "unexpected type" error with the if(input.charAt(i) = '@') statement.
It says it has found a value, but requires a variable. Is the "i" in the FOR loop not a variable?
Please can anybody help me. Any suggestions appreciated.

Whenever I run the program, it is ok if I input an email address among the text. IT displays the email address to screen. However, if no email address is entered (ie. just a string of text), it displays the message "-no valid email". But it still gives me an error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -1
at java.lang.String.charAt(String.java:558)
at CA3a.main(CA3a.java:56)
Here is my code in full:
//I decided to use the '@' character as a reference point, since it is a unique character
//and should not be used in normal sentances. Since an email address is a continous block
//of text with no spaces in it,I created 2 substrings - one from '@' going
//backwards towards the previous space, and one from '@' going forward to the next space.
class CA3a{
   public static void main(String args[]){
//variable declerations
     String input;
     int indexat, length;
     String part1 = "";
     String part2 = "";
//prompt user for input
     System.out.println("This program will detect the 1st email address in a string of text.");
     System.out.println("Please enter a string of text");
//take in input into a stringbuffer
     input = Keyboard.readString();
     StringBuffer buff = new StringBuffer(input);
//assignments
     length = input.length();
     indexat = input.indexOf('@');
//check to see if '@' character is present in string
     if(indexat < 0){
        System.out.println("-no valid email-");
//check for substring part1
     for(int i = indexat; i >= 0; i--){
        if(i == 0){
          part1 = buff.substring(i, indexat);
          break;
        if(input.charAt(i-1) == ' '){
          part1 = buff.substring(i, indexat);
          break;
//check for substring part2
     for(int i = indexat; i <= length; i++){
        if(i == length){
          part2 = buff.substring(indexat, i);
          break;
        if(input.charAt(i) == ' '){
          part2 = buff.substring(indexat, i);
          break;
//output the 2 substring variables to screen.
System.out.println(part1 + part2);
}

Similar Messages

  • I have an iMac running OS 10.4.11. How can I check to see if I have the Flashback Trojan (and remove it, if I have it)? IMy Safari is also crashing frequently. Any suggestions?

    I have an iMac running OS 10.4.11. How can I check to see if I have the Flashback Trojan (and remove it, if I have it)? IMy Safari is also crashing frequently. Any suggestions?

    Hi Barry, is this an Intel iMac, or a PPC iMac?
    Disable Java in your Browser settings, not JavaScript.
    http://support.apple.com/kb/HT5241?viewlocale=en_US
    http://support.google.com/chrome/bin/answer.py?hl=en-GB&answer=142064
    http://support.mozilla.org/en-US/kb/How%20to%20turn%20off%20Java%20applets
    Flashback - Detect and remove the uprising Mac OS X Trojan...
    http://www.mac-and-i.net/2012/04/flashback-detect-and-remove-uprising.html
    In order to avoid detection, the installer will first look for the presence of some antivirus tools and other utilities that might be present on a power user's system, which according to F-Secure include the following:
    /Library/Little Snitch
    /Developer/Applications/Xcode.app/Contents/MacOS/Xcode
    /Applications/VirusBarrier X6.app
    /Applications/iAntiVirus/iAntiVirus.app
    /Applications/avast!.app
    /Applications/ClamXav.app
    /Applications/HTTPScoop.app
    /Applications/Packet Peeper.app
    If these tools are found, then the malware deletes itself in an attempt to prevent detection by those who have the means and capability to do so. Many malware programs use this behavior, as was seen in others such as the Tsunami malware bot.
    http://reviews.cnet.com/8301-13727_7-57410096-263/how-to-remove-the-flashback-ma lware-from-os-x/
    http://x704.net/bbs/viewtopic.php?f=8&t=5844&p=70660#p70660
    The most current flashback removal instructions are F-Secure's Trojan-Downloader:OSX/Flashback.K.
    https://www.securelist.com/en/blog/208193454/Flashfake_Removal_Tool_and_online_c hecking_site
    More bad news...
    https://www.securelist.com/en/blog/208193467/SabPub_Mac_OS_X_Backdoor_Java_Explo its_Targeted_Attacks_and_Possible_APT_link
    Removal for 10.5...
    http://support.apple.com/kb/DL1534

  • TS3899 How do I check to see if the problem resides on the server (e.g., the mail server is not responding) rather than assuming the issue is with my phone and devise setup?

    As of sometime after September 10, 2012, at 4:40PM, I have not been receiving any mail.  Not on my iPhone, iPad, iMac, or MacBookPro.  The trouble shooting help on Apple's web site is designed to help ensure my devise settings are accurate.  However, I moved from .Mac to .me and now to .cloud and have operating with no issues for several months on the iCloud.
    How can I check to see if my mail problem is really Apple's fault rather than double checking all my devise settings ad nausiam?  I feel there is a server issue with Apple because none of my devises can receive mail, however, I do not know how to validate my theory with Apple.  I get an error message telling me:
    Cannot Get Mail
    The connection to the server failed.
    Apple says there is a mail server problem affecting 1% of users.  However, am I one of the 1%?  If I assume I am, wait for the problem to be fixed, and then learn I was not one of the 1% and continue to have mail issues, then I will be more frustrated.

    I have the same problem since yesterday morning. I checked the systems status and still the same. I guess we are the 1.1% who has problem with their email.

  • I am new to mac, but i keep reading about Mountain lion.  I just bought my Mac book Pro less then a month ago.  my current software is OS X 10.8.2, Do i already have mountain lion?  How do I check to see?

    I am new to mac, but i keep reading about Mountain lion.  I just bought my Mac book Pro less then a month ago.  my current software is OS X 10.8.2, Do i already have mountain lion?  How do I check to see?

    As the others have stated, you're on Mountain Lion with 10.8, anything. But you can check your version of the software by going to the apple in the upper left corner of the screen and clicking it and selecting "About This Mac". That will open a box that shows your software version. Once there you can click on "More Info" that will tell you everything about your specific mac configuration.
    Welcome to the Mac family.

  • How can I check and see if someone is tracking online activity on my IPad 3 from their device?

    How can I check to see if someone is tracking or accessing my Internet activity on my Ipad3 from a separate location?

    You can't. Make sure you have security/firewall set on your router. If you are using your iPad in a free wifi location (ie like Starbucks) it's possible for someone to intercept your wifi activity.
     Cheers, Tom

  • How can I check to see if there is money left on an iTunes card if I can't read the scratch off code?

    How can I check to see if there is money left on an iTunes gift card if I can't read the scratch off code?

    Sorry, but other than trying to redeem the card there's no way to check whether it is still valid or was previously redeemed.
    Regards.

  • HT1212 If I never synced to itunes and I restore the device. Will I lose unstored data? How can I check to see the last time my phone was backed up if I forgot the passcode to my phone?

    If I never synced to itunes and I restore the device. Will I lose unstored data? How can I check to see the last time my phone was backed up if I forgot the passcode to my phone?

    If you can't remember your passcode, you'll need to restore your phone.  If you never synched to iTunes, all data on your phone will be erased.  No way around this.

  • How can I check to see if my version of QuickTime Pro 7.6.6 already has ProApps QuickTime Codecs installed? I also have Final Cut Pro 6. I'm asking because my computer can't seem to play ProRes 422 codec video without stuttering or skipping?

    I have a fairly capable MacBook Pro (2.93 GHzIntel Core 2 Duo processor, 4GB 1067 MHz DDR3 memory and a 1TB HD -5400 rpm, so I am hoping it's not that my computer that can't support it and it's something more simple like not have ProApps QuickTime codecs installed. Thanks in advance!

    How can I check to see if my version of QuickTime Pro 7.6.6 already has ProApps QuickTime Codecs installed? I also have Final Cut Pro 6. I'm asking because my computer can't seem to play ProRes 422 codec video without stuttering or skipping?
    If you can view a ProRes 4-2-2 video at all, then you either have the independent ProRes422 Playback component installed or the FCP "Pro" video components installed.
    I have a fairly capable MacBook Pro (2.93 GHzIntel Core 2 Duo processor, 4GB 1067 MHz DDR3 memory and a 1TB HD -5400 rpm, so I am hoping it's not that my computer that can't support it and it's something more simple like not have ProApps QuickTime codecs installed.
    Check your "~/Library/QuickTime" folder for your various "Pro" components. If in doubt that you have them all, re-run the FCP installer and it should add any missing components, apps, or utilities.

  • How can I check and see that my photos are being backed up by time machine?

    How can I check and see that my photos are being backed up by time machine?

    http://pondini.org/TM/8.html

  • How can I check to see if my iPad is factory unlocked

    Hi, I'm having problem using a sim in my iPad when there is no Wi-Fi. I have been told that iPads lock to the first sim that is installed. As I have now changed my network provider, could this be the reason my new sim does not work?
    If this is the case, how can I check to see if the iPad is still unlocked, as I do not have another sim card to try. More importantly, How do I unlock my iPad???
    Thanks for your help
    RM

    The only iPads that are unlocked (for use in the US) are iPads that shipped with the Apple Sim card.  I believe that is the iPad Air 2 and the iPad Mini with Retina.  The other iPads are locked to the carrier they were purchased for use with, and that cannot be changed.  The only exception would be in using a sim card in another country. 
    If it is an AT&T iPad, you have to use AT&T.  Same for Sprint, Verizon & T-Mobile.

  • How do I check to see if my reinstalled CS6 Web Design program has been activated?

    I installed CS6--from a trial download--on my reformatted laptop. I deactivated the program before reformatting the laptop. I then filled in the serial number from my purchased CS6 Web Design program. The trial version (which lists as CS6 Master Collection) started, but I get screens asking if I want to update my trail version. I can not get help from Adobe Customer Care (800-833-6687) because they do not support Creative Suite. The telephone system referred me to the web system (www.adobe.com/getsupport), which does not allow me connection to Live Chat. It refers me to the community forum. So, guys, how do I check to see if my reinstalled CS6 Web Design program has be activated?

    When I click on the link I just gave you, this is what I see:
    Your last message was ambiguous about whether you had actually talked to Web Chat.
    I've been told that Web Chat is handled from the US time zones so if you're in a different part of the world, you may have to accommodate that.
    Serial number issues cannot be handled in a forum because (obviously), you're communicating a serial number which would  be broadcast to the entire internet. Also, few Adobe employees are on most of the forums. There are NO Adobe employees on this forum.

  • How do i check and see what is using up all my harddrive space

    How do i check to see what is using all of my hard drive space?

    I would try OmniDiskSweeper.
    Matt

  • HT5035 how can i check or see the remaning balance that i have in my itunes account

    how can i check or see the remaning balance that i have in my itunes account

    On what device/computer?

  • How can I check to see if my software has been registered?

    After a recent hardware replacement I am unable to find my Aperture 2 download. I never used it anyway so i'm not concerned but would like to be able to pass it on if it can be, is this possible? How can I check to see if I even registered it? I have the original packaging and all contents including Serial numbers, support ID, and four software coupons for updates. I am a bit technophobe, sorry!
    So,
    1. How can I check to see if it was registered correctly?
    2. Can someone else use the software?
    Hope someone can help.

    You don't need to check registration. In the Apple world registering software really doesn't have much of a purpose.
    If the software was bought on DVD and you have the serial number you are free to give it or sell it to anyone you wish.  Just keep in mind that Aperture 2 is very old and while it (sort of) works on the newer OS's it really isn;t a good solution for anyone serious about using Aperture. 
    It would be OK for someone with older hardware that is unable to upgrade past Snow Leopard but for anyone with newer hardware it would be a mistake to use.

  • HT1688 how do i check to see if everything backed up

    How do i check to see if my contacts are backup?

    Contacts are not included in the backup.  They are designed to be synced to a supported application on the computer or a cloud service.

Maybe you are looking for

  • How to take back up of my XI scenraios ( namespaces and Objects )

    Hi I have done some scenarios on XI sytem which has been crashed. Present it is not working . J2ee Engine is not starting. Now we have installed new XI system. I want to take backup of my old scenarios and i need to import it into new XI system. How

  • How to map Write-Off details to GL

    Hi, I'm using cst_write_offs,cst_write_off_details tables in one of my query. I want to know how we can map these tables to the GL tables. Scenario: Need to fetch rcv_transaction_id and po_distribution_id from the above write-off tables. I am getting

  • In a Mail message, can we default to show "details"?

    In order to see the cc list when reading a message, I now have to click on "details." Is there a way of making that the default? I didn't see anything applicable in preferences. Thanks, doug

  • Find Whether Array Element have same element in all index

    Hi , I need to check if an array have the same element in all its indexes.(Example: An Array of Size 4, should have index 0 ..index 3 as 1 and during next iteration let it be number 2 in all indexes......how do I check if all index have same number)

  • Unable to "double-click"

    After having issues with Leopard and FCP, I downgraded back to Tiger but now I am unable to use my double click feature, as well as the "right click" feature on my touch pad. Therefore I am unable to open my links in new tabs or save pictures, etc. I