Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: URGENT HOMEWORK HELP!

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default URGENT HOMEWORK HELP!

    So for my homework I am supposed to create a program that allows a user to input a Twitter/Facebook status with a mention (@), a hashtag (#) and a URL link to some website. The program is supposed to recognize these three things, extract them and display them as follows:
    hashtag: homework
    usertag: Mary
    URL: http://www.facebook.com/NikolaCS110

    We are also supposed to use the ternary operator, but I'm stumped as to how to incorporate that. This is what I have so far, but I am seriously stuck! I've tried to just run the program with it recognizing and printing out just the #, but it gives me an error message which I assume will happen for the other two things

    here's what I have so far
     
    import java.util.Scanner;
    public class SocialMedia {
    	public static void main (String [] args){
    		Scanner input = new Scanner(System.in);
    		String userStatus = null;
    		String userTag = null;
    		String userHashtag = null;
    		String userUrl = null;
    		int mention = 0;
    		int hashtag = 0;
    		int link = 0;
     
    		System.out.println("Enter a status with a link, a hashtag, and a mention");
    		userStatus = input.nextLine();
     
    		mention = userStatus.lastIndexOf('@');
    		hashtag = userStatus.lastIndexOf("#");
    		link = userStatus.lastIndexOf(':')
     
     
                     userHashtag = userStatus.substring(hashtag);
     
    		System.out.println("hashtag:" + userHashtag);

    and here is the error message
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at SocialMedia.main(SocialMedia.java:22)


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: URGENT HOMEWORK HELP!

    Try to figure out where the -1 come from

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HOMEWORK HELP!

    I fixed the error message by changing the substring to
     userHashtag = userStatus.substring(0,hashtag +1);
    , but it wont display the text after the #

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: URGENT HOMEWORK HELP!

    That's a terrible title, and it's going to turn a lot of people away from helping you. Please see the link in my signature on asking questions the smart way before you post again.

    You need to step through this with a debugger, or at least add some print statements, to figure out what's going on.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: URGENT HOMEWORK HELP!

    Quote Originally Posted by mpagudelo View Post
    I fixed the error message by changing the substring to
     userHashtag = userStatus.substring(0,hashtag +1);
    , but it wont display the text after the #
    That does not solve the problem, it patches the symptom.
    Figure out where the -1 come from to begin with.

Similar Threads

  1. Help with homework
    By gta1 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 11th, 2013, 08:58 PM
  2. Help with my homework?
    By mshem in forum Java Theory & Questions
    Replies: 2
    Last Post: November 16th, 2012, 03:15 AM
  3. Homework help?
    By regi.dg in forum Object Oriented Programming
    Replies: 0
    Last Post: October 16th, 2012, 08:17 PM
  4. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  5. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 11:09 AM

Tags for this Thread