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)