reading a keyboard text input character by character

Hello,

can anyone give me help with reading a string (inputed from the keyboard) character by character?

I am just starting this code so i dont have any code atm.

i just need some pointers about how to go about reading character by character.

edit: i dont think i am clear enough.

I wish to read a string. and then character by character find out if the character is a vowel, constanant or a space. and then also count the number of characters

Any help will be greatly appriciated. thanks

Hypercondor

Message was edited by:

Hypercondor

[598 byte] By [Hypercondora] at [2007-11-27 4:11:36]
# 1
String a="text";char[] tabC = a.toCharArray() ;for(int j=0;j<tabC.length ;j++){//process with chars}RegardsMarecki>
marecki6000a at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 2

Try this code :

import java.util.Scanner;

public class ReadDemo {

static char[] vowel={'a', 'e', 'i', 'u', 'o', 'w', 'y'};

public static void main(String[] args) {

System.out.println("Enter a word:");

Scanner scanner = new Scanner(System.in);

String word = scanner.next();

System.out.println("Entered Word: "+word);

System.out.println("# of characters: "+word.length());

System.out.println("# of vowels: "+countVowels(word));

}

static int countVowels(String s){

char[] array = s.toCharArray();

int counter=0;

for (int i = 0; i < array.length; i++) {

if(isVowel(array[i])){

counter++;

}

}

return counter;

}

static boolean isVowel(char c){

for (int i = 0; i < vowel.length; i++) {

if(c==vowel[i]){

return true;

}

}

return false;

}

}

java_2006a at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 3
If i put the text into array. say i had "hello my name is hypercondor". Would that not put all the text into one section of the array and not split it up?Regards hypercondor
Hypercondora at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 4

import java.util.Scanner;

public class ReadDemo {

static char[] vowel={'a', 'e', 'i', 'u', 'o', 'w', 'y'};

public static void main(String[] args) {

System.out.println("Enter a word:");

Scanner scanner = new Scanner(System.in);

String word = scanner.next();

System.out.println("Entered Word: "+word);

System.out.println("# of characters: "+word.length());

System.out.println("# of vowels: "+countVowels(word));

}

static int countVowels(String s){

char[] array = s.toCharArray();

int counter=0;

for (int i = 0; i < array.length; i++) {

if(isVowel(array[i])){

counter++;

}

}

return counter;

}

static boolean isVowel(char c){

for (int i = 0; i < vowel.length; i++) {

if(c==vowel[i]){

return true;

}

}

return false;

}

}

Thank you very much for this. Alot more than i asked for. Although i need to be able input more than one word. Also would i have to use the Hex code to make it check for spaces?

Anymore help would be greatly appriciated.

Hypercondora at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 5

Try this:

import java.util.Scanner;

public class ReadDemo {

static char[] vowel={'a', 'e', 'i', 'u', 'o', 'w', 'y'};

public static void main(String[] args) {

System.out.println("Enter a word:");

Scanner scanner = new Scanner(System.in);

String word = scanner.nextLine();////////////////////////CHANGE HERE

System.out.println("Entered Word: "+word);

System.out.println("# of characters: "+word.length());

System.out.println("# of vowels: "+countVowels(word));

System.out.println("# of spaces: "+countSpaces(word));

}

static int countSpaces(String s){

return s.split(" ").length;

}

static int countVowels(String s){

char[] array = s.toCharArray();

int counter=0;

for (int i = 0; i < array.length; i++) {

if(isVowel(array[i])){

counter++;

}

}

return counter;

}

static boolean isVowel(char c){

for (int i = 0; i < vowel.length; i++) {

if(c==vowel[i]){

return true;

}

}

return false;

}

}

java_2006a at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 6

in order to count spaces, use this instead:

static int countSpaces(String s){

int count=0;

char[]a=s.toCharArray();

for (int i = 0; i < a.length; i++) {

if(32==(int)a[i]){ //32 = (int)' '

count++;

}

}

return count;

}

java_2006a at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 7

Thank you so much. Truely you are a legend.

This is my code at the moment.

import java.util.Scanner;

public class ReadDemo

{

static char[] vowel={'a', 'e', 'i', 'u', 'o', 'w', 'y'};

public static void main(String[] args)

{

System.out.println("Enter a word:");

Scanner scanner = new Scanner(System.in);

String text = scanner.nextLine();

System.out.println("Entered Text: "+text);

System.out.println("# of characters: "+text.length());

System.out.println("# of vowels: "+countVowels(text));

System.out.println("# of constanants: "+(text.length()-countVowels(text)-countSpaces(text)));

System.out.println("# of spaces: "+countSpaces(text));

}

static int countVowels(String s)

{

char[] array = s.toCharArray();

int counter=0;

for (int i = 0; i < array.length; i++)

{

if(isVowel(array[i])){

counter++;

}

}

return counter;

}

static boolean isVowel(char c)

{

for (int i = 0; i < vowel.length; i++)

{

if(c==vowel[i])

{

return true;

}

}

return false;

}

static int countSpaces(String s){

int count=0;

char[]a=s.toCharArray();

for (int i = 0; i < a.length; i++) {

if(32==(int)a[i]){ //32 = (int) for a ' '

count++;

}

}

return count;

}

}

Now all i gotta do is make a list of the frequency with which each vowel appears in the string as a percentage of the number of printing characters. I think i need a array to do this. also i need to limit the input length to 300 characters.

Hypercondora at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 8

static int countSpaces(String s)

{

int count=0; // Counter set to 0

char[]a=s.toCharArray();

for (int i = 0; i < a.length; i++)

{

if(32==(int)a[i]){ //32 = (int) for a ' '

count++; // count + 1

}

}

return count;

I understand the majority of this code. But am not completely certain how it realises its a space. I understand that 32 is the integer value for a space. but i dont understand how Java knows its the integer for a space.

As always having the code is great. But it has to be backed up with knowledge. Could you please explain.

Regards

Hyper.

Hypercondora at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 9

Run this example:

public static void main(String[] args){

for(int i=0; i<255; i++){

System.out.println(i+"->"+(char)i);

}

}

this will list the first 255 characters with their decimal code.

you can also do this:

public static void main(String[] args){

int i=32;//decimal code for simple space : ' '

System.out.println(i+"->"+(char)i);

}

Hope that helps!

java_2006a at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 10

Thank you very much for the help. I have one more question.

could you describe what this means please?

char[]a=s.toCharArray();

within

static int countSpaces(String s){

int count=0;

char[]a=s.toCharArray();

for (int i = 0; i < a.length; i++) {

if(32==(int)a[i]){ //32 = (int)' '

count++;

}

}

return count;

}

Hypercondora at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...
# 11

char[]a=s.toCharArray();

this creates a characters array of the string s.

Example:

String s = "abc";

char[] a = s.toCharArray();//or char a[] =s.toCharArray(); //a = {'a', 'b', 'c'}

for(int i=0; i<s.length, i++){

System.out.println(a[i]);

}

results:

a

b

c

Hope That helps>

java_2006a at 2007-7-12 9:17:27 > top of Java-index,Java Essentials,Java Programming...