Question:
I want to read a String from console and use it character by character? In Java?
2010-10-17 23:37:57 UTC
In Java I know the use of Scanner class to read a line of text from console ,but i want to read the string character by character so that i can put those chars in an array .(I am trying String Palindrome Program) please help me out.
Five answers:
2010-10-17 23:51:24 UTC
This should do it for you:



import java.io.*;

/**

* Demonstrate reading characters from the console using

* the standard input from System.in wrapped with

* InputStreamReader.

**/

public class SingleCharIOApp

{

public static void main (String arg[]) {

char ch;

int tmp = 0;



// System.in std input stream already opened by default.

// Wrap in a new reader stream to obtain 16 bit capability.

InputStreamReader reader = new InputStreamReader (System.in);



// System.out std output stream already opened by default.

// Wrap in a new writer stream to obtain 16 bit capability.

OutputStreamWriter writer = new OutputStreamWriter (System.out);



try {

// Hit CTRL-Z on PC's to send EOF, CTRL-D on Unix.

// The EOF puts -1 = 0xFFFFFFFF into integer.

while (tmp != -1 ) {

// Read a single character from keyboard

tmp = reader.read ();



// A 1 byte character is returned in integer.

// Cast to char to get character in low 2 bytes.

ch = (char) tmp;



// If a symbol character with 2 byte unicode read in,

// the will see 4 hex output

writer.write ("echo " + ch

+ " whose code = "+ Integer.toString (tmp,16) + '\n');



// Need to flush the OutputStream buffer to get the

// characters sent to the screen.

writer.flush ();

}

}

catch (IOException e) {

System.out.println ("IO error:" + e );

}

} // main

} // class SingleCharIOApp
Lucho
2010-10-17 23:56:04 UTC
Part 1:

Google: "java read string"[1]



=> First item "java String reading"[2]



System.out.println("Type some text and press 'Enter.'");



String string = "";

InputStreamReader input = new InputStreamReader(System.in);

BufferedReader reader = new BufferedReader(input);



  // read in user input



  try{

       string = reader.readLine();

  } catch(Exception e){}



System.out.println("You typed: " + string);







Part 2:

To get an array of bytes from the string:

Googlle: "java String as Bytes"[3].



Result[4]:



String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";



byte[] theByteArray = stringToConvert.getBytes();

--
Mohit
2010-10-17 23:47:59 UTC
The String Palindrome program can be done without using a char array...



public static void main(String s[])

{

String st = s[0];

String rev = "";



//generating reverse of the string

for(int i = st.length() - 1; i >= 0; i--)

{

rev += "" + st.charAt(i);

}



if(rev.equals(st)) //or you can use equalsIgnoreCase()

System.out.println("It is a palindrome");

}
?
2010-10-17 23:55:36 UTC
First read the string from the console...

Then using the for loop, copy the string into the array one by one by using String.charAt(index) method..



Ex:



String str="abcdefgh";

char a[]=new a[10];

for(int i=0;i
a[i]=str.charAt(i);
deonejuan
2010-10-18 01:57:32 UTC
import java.util.Scanner;



public class PallyChars {



public static void main( String [] args ) {

Scanner sc = new Scanner( System.in );

System.out.print("Enter your string => ");

String in = sc.nextLine();

char[] chars = in.toLowerCase().toCharArray();

char[] pally = new char[ chars.length ];

int count = -1;

for (int i = pally.length-1; i>=0; i--) {

pally[++count] = chars[i];

}

boolean isPally = String.valueOf(pally).equals(String.valueOf(in));

System.out.println("Is it a Pally? "+

String.valueOf(in) +" <=> " +

String.valueOf(pally) + " == " + isPally);

}

}



// the .equals() works in this case because it compares Strings. Will not work on char[] c .equals char[] d.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...