in java how can one check if there are spaces after a char?
Well, how is it being stored or taken in?
i am trying to check if an input has that char but it will only count it if there are no spaces afterwards
i am doing a tweet tester, checking if there are # but it will only count the # if there are no ' ' after or no '\t'
You can do a regex.
i dont know what that is :(
Regular Expressions. They can match white space. http://docs.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
If you're using characters as your data type, then there won't be any spaces after it so you might want to consider this as a way to check. Here's a super silly way to do it based off this with spaces ``` String cStr = "c"; char[] cAndSpaces = cStr.toCharArray(); char c = cAndSpaces[0]; ``` So I made a string with c and spaces after it. Then I used the String method toCharArray() to create an array. Then I used the first element, which in arrays is the zeroth element I guess you can call it, which is in the array. A quicker way would be ``` String cStr = "c"; char c = cStr.toCharArray()[0]; ``` Another possible way to do it is check the length of the string. ``` String cStr = "c"; if (cStr.length() == 1){ System.out.println("no spaces!"); } ``` I'm almost sure neither of these are going to help you directly since your question is too vague, but maybe it will help to think about it.
this is my code import java.util.Scanner; class tbesting { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("Please enter a tweet: "); String input = scan.nextLine(); int length = input.length(); int count = 0; int hashtags = 0, attributions = 0, links = 0; char letter; boolean tf; char s = ' '; char t = '\t'; String linkss = "http://"; if (length > 140) { System.out.println("Excess characters: " + (length - 140)); } else { while (count < length) { letter = input.charAt(count); { if ((letter =='#') && (letter + 1!= '\t') && (letter + 1!= ' ')) { hashtags ++; count ++; } } if (letter == '@' && letter + 1 != ' ' && letter + 1 != '\t') { attributions ++; count ++; } if (letter == 'h') { // String test = input.substring(count,count+6); // test = test.toLowerCase(); if (input.toLowerCase().contains(linkss.toLowerCase())) // boolean link = input.equalsIgnoreCase(linkss); // if (input.startsWith("http://", count)|| link == true) { links ++; count++; } else { count++; } } else { count ++; } } System.out.println("Length Correct"); System.out.println("Number of Hashtags: " + hashtags); System.out.println("Number of Attributions: " + attributions); System.out.println("Number of Links: " + links); } } }
The problem is you need to be sure that there is even is a next char before finding out what that next char is.
In your case though, you are using `letter + 1` when you mean to say `input.charAt(count+1)`.
You should check that `count+1<length` as well.
Join our real-time social learning platform and learn together with your friends!