Created
October 28, 2015 11:59
-
-
Save edinak1/a40fee34dcc10ec2ac26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| public class Lesson2 { | |
| public static void main(String[] args) { | |
| System.out.println(getFirstCharacterOfWord(null,2)); | |
| System.out.println(getFirstCharacterOfWord(" ",2)); | |
| System.out.println(getFirstCharacterOfWord("ahjkji",1)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",0)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",5)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",2)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",3)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",4)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",5)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",6)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",7)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",8)); | |
| System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",20)); | |
| } | |
| static char getFirstCharacterOfWord(String str,int numberWord) | |
| { | |
| //proverka na sushestvovanie stroki | |
| if(str==null || str=="") | |
| { | |
| System.out.println("String is not found"); | |
| return 0; | |
| } | |
| //proverka na nomer slova>0 | |
| if(numberWord<1) | |
| { | |
| System.out.println("Index not correct"); | |
| return 0; | |
| } | |
| //proverka na nomer slova > kolichestva slov v stroke | |
| int index=0,k=1; | |
| while(str.lastIndexOf(" ")>index) | |
| { | |
| index=str.indexOf((" "), index)+1; | |
| if(str.charAt(index)!=' ')//kolichestvo slov uvelichetsya,esli posle probela ne stoit snova probel | |
| k++; | |
| } | |
| if(k<numberWord) | |
| { | |
| System.out.println("Index > number of words"); | |
| return 0; | |
| } | |
| index=0;k=1; | |
| while(true) | |
| { | |
| if(numberWord==k) | |
| return str.charAt(index); | |
| index=str.indexOf(" ",index); | |
| //Proverka esli stroka tolko iz probelov ili bolshe odnogo probela megdu slovami | |
| while(str.charAt(index)==' ') | |
| { | |
| if(index+1==str.length()) | |
| { | |
| System.out.println("String have not any word"); | |
| return 0; | |
| } | |
| index++; | |
| } | |
| k++; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment