There are two approaches, you can either take exactly one character or strictly one character. When you use exactly, the reader will take only the first character, irrespective of how many characters you input. For example: import java.util.Scanner; public class ReaderExample { public static void main(String[] args) { try { Scanner reader = new Scanner(System.in); char c = reader.findInLine(".").charAt(0); reader.close(); System.out.print(c); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }When you give a set of characters as input, say "abcd", the reader will consider only the first character i.e., the letter 'a' But when you use strictly, the input should be just one character. If the input is more than one character, then the reader will not take the input import java.util.Scanner; public class ReaderExample { public static void main(String[] args) { try { Scanner reader = new Scanner(System.in); char c = reader.next(".").charAt(0); reader.close(); System.out.print(c); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }Suppose you give input "abcd", no input is taken, and the variable c will have Null value.
Scanner class in Java supports nextInt(), nextLong(), nextDouble() etc. But there is no nextChar() (See this for examples) To read a char, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.
Input : gOutput : c = gThis article is contributed by Piyush Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above Article Tags :
We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows: Illustration: Input: Input custom string = Hello Output: --> String to be added 'Geeks' --> If end position, Output: HelloGeeks --> If in beginning, Output: GeeksHello --> If at sat 3rd index, Output: HelGeeksloMethods: This can be done using multiple methods of which frequently used methods are listed below as follows:
Let us discuss all three methods above listed in detail to get a fair understanding of the same Method 1: Using + operator 1.1 At the end Example: One can add character at the start of String using the ‘+’ operator.
1.2 At the beginning Example: One can add character at the start of String using the ‘+’ operator.
Method 2: Using insert() method of StringBuffer class StringBuffer is a peer class of String that provides much of the functionality of strings. The string represents fixed-length, immutable character sequences while StringBuffer represents grow able and writable character sequences. StringBuffer may have characters and sub-strings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters pre-allocated than are actually needed, to allow room for growth. One can use the StringBuffer class method namely the insert() method to add character to String at the given position. This method inserts the string representation of given data type at given position in StringBuffer. Syntax: str.insert(int position, char x); str.insert(int position, boolean x); str.insert(int position, char[] x); str.insert(int position, float x); str.insert(int position, double x); str.insert(int position, long x); str.insert(int position, int x); position is the index in string where we need to insert.Return type: A reference to this object. Example
Method 3: Using substring() method One can also use String’s substring method to add character to String at given position. This method has two variants, and it returns a new string hat is substring of a string where the substring begins with a character at the specified index and extends to the end of the string. Syntax: public String substring(int begIndex)Parameters: The beginning index, inclusive. Return Value: The specified substring. Example
Article Tags :
In this tutorial, we will learn about how to create a program in C that will print next successive character and adjacent character. Here first we have created a program that will ask from user to enter any character to find and print its next successive character. Also we have created a program that will take a character as input (from user at run-time) and then will print its adjacent character. C Print Next Successive CharacterLet's first create a program that will print next successive character of given character by user. Let's suppose that if user has entered a as character input, then program will print b (the next successive character of a). Or if user will provide C as character input, then program will print D as output. The question is, write a program in C, to accept a character from user and process it in these two ways:
The answer to this question is given below: #include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); printf("\n"); if(ch>=65 && ch<90) printf("%c", ch+1); else if(ch>=97 && ch<122) printf("%c", ch+1); else if(ch==90) printf("%c", 65); else if(ch==122) printf("%c", 122); else printf("%c", ch); getch(); return 0; } As the program given above was written under the Code::Blocks IDE, therefore after successful build and run, here is the sample run:
Now supply any character as input say a and press ENTER key to see the next character that comes after a. Here is the second snapshot of the sample run:
Let's check the same program with another sample run. This time provide the input as Z and press ENTER key:
Here is another sample run that checks what will happen if user supplies any input that is not an alphabet character:
Program Explained
C Print Adjacent CharacterLet's create another program that will print adjacent character of any given character by user at run-time. Here adjacent characters are the two character, one comes before and second comes after the given character. For example, if user has entered A, then print its adjacent character as Z and B. Or if user has entered D, then print C and E #include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); printf("\n"); if(ch>65 && ch<90) printf("Adjacent characters = %c and %c", ch-1, ch+1); else if(ch>97 && ch<122) printf("Adjacent characters = %c and %c", ch-1, ch+1); else if(ch==90) printf("Adjacent characters = %c and %c", ch-1, 65); else if(ch==122) printf("Adjacent characters = %c and %c", ch-1, 97); else if(ch==65) printf("Adjacent characters = %c and %c", 90, ch+1); else if(ch==97) printf("Adjacent characters = %c and %c", 122, ch+1); else printf("%c", ch); getch(); return 0; } Here is the final snapshot of sample run:
The concept used in above program is similar as used in previous program. Except that we have to print the previous character along with next character. Therefore we have used ASCII code for A, a, Z, and z in separate cases. C Online Test « Previous Program Next Program » |