answersLogoWhite

0


Best Answer

Reading text from the console in Java is easy--if you know the language.

Here's one way, you can create a class with the following code:

import java.io.*;

class lineScanner {

private static String prompt;

public lineScanner(String aPrompt) {

setPrompt(aPrompt);

}

public void setPrompt(String aPrompt) {

prompt=aPrompt;

}

public static String lineIn() throws IOException {

String CurLine = "";

System.out.println(prompt);

InputStreamReader converter = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(converter);

CurLine = in.readLine();

return CurLine;

}

}

This class can be used to create an object that reads text. Here's how it works:

create the object by using

lineScanner in = new lineScanner("a string");

And by using the lineScanner object you've just created, you can input the text like this:

string1 = in.lineIn();

This will set the variable named string1 to text that is entered from the keyboard. When you have tried this out, you may have realized that the computer displays a short prompt message, "a string", when text is inputted.

This is because when the object created, the message was set to "a string" by the constructor. You can change this message after it is set using

in.setPrompt("a different string);

If you want to see the program in action, create another class with the following code:

import java.io.IOException;

/**

* @author yourName

*/

public class mainClass {

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

lineScanner in = new lineScanner("Please enter a line.");

String inBuffer = null;

while (inBuffer != "exit") {

inBuffer = in.lineIn();

System.out.println("You entered: " + inBuffer);

}

}

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

To add a Character to a string, simply use concatenation. When adding to strings, the + sign concatinates.

Example:

String one = "This is a strin";

one = one +'g';

/*OUTPUT :

This is a string

*/

Note, that if you intend to modify a string frequently, you should NOT be using the String class. The String object is intended for fixed strings which never (or, extremely rarely) change contents. If you have a string in your program which you intend to modify frequently, use the StringBuffer class instead. It operates in the same manner as String, but the underlying implementation is completely different, and is tuned for faster string manipulation operations.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

A String is nothing but a bunch of characters one after the other whereas a character has a size of only '1'

So converting a string into a char is not possible. what you can do is form a character based on only one element from the string.

Ex:

String name = "Rocky";

char xyz = name.charAt(0);

now the char xyz will have one character from the String.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to add a character to a string in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What does it mean to have a string split in java?

To have a string split in Java means that a string array, containing substrings (can be delimited by elements of a specified string or Unicode character array), is returned.


What is java string?

array of character data type which is terminated by null character


In java is A string the same thing as a char?

No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.


Is special symbols are valid as a string in java?

Yes. Any special character inside the String is considered as part of the string variable and would not be treated as a special character. Ex: String str = "ABC_$4"; is a valid string declaration


How do you encrypt a string in java by replacing the characters with the next alphabet?

A "for" loop, looping through each character, should work just fine. Just get one character at a time, add one to it, and add it to the end of the new string. Using a String object for the new string should work, but it is inefficient for large strings; in this case, a StringBuffer provides better performance. By the way, it should be noted that this is not a very secure encryption.

Related questions

What is java string?

array of character data type which is terminated by null character


What does it mean to have a string split in java?

To have a string split in Java means that a string array, containing substrings (can be delimited by elements of a specified string or Unicode character array), is returned.


What is a string in java?

An object that stores an ordered set of characters (ie. "hello"). The String class represents character strings.


In java is A string the same thing as a char?

No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.


Is special symbols are valid as a string in java?

Yes. Any special character inside the String is considered as part of the string variable and would not be treated as a special character. Ex: String str = "ABC_$4"; is a valid string declaration


How do you encrypt a string in java by replacing the characters with the next alphabet?

A "for" loop, looping through each character, should work just fine. Just get one character at a time, add one to it, and add it to the end of the new string. Using a String object for the new string should work, but it is inefficient for large strings; in this case, a StringBuffer provides better performance. By the way, it should be noted that this is not a very secure encryption.


String?

For the majority of languages, a string is a primary data type that denotes an ordered series of one or more characters (a character is an item that can be input or output - in general, they are displayable in some form, but there are character systems which have non-displayable characters for reasons of history). One character can only be represented by a single 8-bit byte in some languages (like C), which corresponds to an ASCII code entry. A single character (char) and a string (really an array of characters ended by a NULL (zero) character) both have their data types in the programming language C. Other languages, like Python 3, for instance, define a character as any Unicode item ranging in size from one to four bytes. The runtime environment (JRE), made up of the JVM, is a general-purpose, concurrent, object-oriented, class-based programming language called Java. We shall be talking about Java String, a novel idea, in this blog. Each character in a string is a separate unit. A line, however, is an object that represents a series of characters in Java. A string object is made using the a class named String. A String object can be created in given ways: Using a literal string Java, Double quotes are used to produce string literals. For instance: s="Welcome" string; Using a new keyword When creating a Java String, the "new" keyword is used. For instance: s = new String ("Welcome"); It produces two objects (in a heap and the String pool), as well as one reference variable whose value, "s," refers to the object in a heap. Let's now examine the Java String pool concept. Java String Pool A group of Strings that are kept in heap memory collectively comprise the Java String pool. String Pool initially determines whether the item is already present in the pool or not whenever a new object is formed. If it is, the variable receives the same reference back; otherwise, a new object will be generated Java String Methods Java String length(): The Java String length() function provides the string's length information. It gives the total number of characters in the String as a count. Java's compareTo(): The given string is compared to the current string using the Java String compareTo() function. It is a method of the "Comparable" interface that the String class implements. Java String concat(): This method joins a particular string to the end of other string and then outputs the resulting combined string. It is comparable to adding a new string. Java String IsEmpty(): This method determines whether or not the String is empty. It returns true if the java String is Empty and false otherwise. toLowerCase() : All of the characters in the String are converted to lowercase using the Java String toLowerCase() function. Java's toUpper() method: All of the characters in the String are changed to the upper case via the Java String toUpperCase() function. Java String Replace(): This method returns a string with all the old characters or characters in a CharSequence replaced with new characters. contains(): The Java contains() method looks through the string's characters in order. It returns true if the character sequences are detected; otherwise, it returns false. String to CharArray() in Java: This method turns the given Java String into a character array by first calculating the length of the string, including any spaces, and then producing an array of the same name as a char type. String IsEmpty() in Java: This method determines whether or not the String is empty. The String returns true if its length is zero; otherwise, it returns false. StringBuffer and StringBuilder are two utility classes offered by Java. Let's examine what makes these two utility classes distinct from one another: Mutable classes include StringBuffer and StringBuilder. In contrast to StringBuilder operations, which are not thread-safe, StringBuffer operations are synchronized. StringBuffer should be used in the single-threaded environment when many other threads are working on the same String and StringBuilder. StringBuilder performs faster than StringBuffer Because there is no synchronization overhead, That is all there is to know about string for a novice. I hope this article answers your query.


What is the significance of the character string T0526?

The significance of he string T0526 is the functioning of the ECMA script. This is needed when working with Java and programming and used to enhance webpages.


How do you convert char to String in java?

A String is nothing but a bunch of characters one after the other whereas a character has a size of only '1' So converting a string into a char is not possible. what you can do is form a character based on only one element from the string. Ex: String name = "Rocky"; char xyz = name.charAt(0); now the char xyz will have one character from the String.


What is the character string usually called?

I think, array of string type object in java.actually java take any command line argument in stringformat. bydefault


What is the different between char and string?

char is used in Java to enetr only one character but string is used to specify a whole sentence or a bunch of characters.


What are some example for string formatting in Java?

There are lots of examples of string formatting in Java. It can be difficult at times. Some of these examples are, but are not limited to; align, string, format, and JAVA.