the output of this program should return the number of letter in words

 * @(#)
 * @author
 * @version 1.00 2011/11/18

import java .util.Scanner;
public class Coun {
  Scanner input=new Scanner(;
      public static void main(String [] args)  {
     System.out.println("Enter a string: ");
    String[] arr =  input.nextInt();
    int length = arr.length;
    System.out.println ("Lenght of string: "+arr+" is:"+length);
is there any ideas about other case

Recommended Answers

All 5 Replies

are u making a transition from c++?? "hehe".

anyway, in Java a string is stored in a single String object and not a String array. You use character array in C++ [pre Ansi string object era].

and so donot use the "[]"

and also... refer the documentation of Scanner class to see how to read a string from the console and debug your program.

Good luck!

If I am understanding your question correctly you just want the output to be the length of the string that the user enters? If so you do not need an array.

Some of the problems I see is in line 14 you have declared an array of strings and then using input.nextInt(). Those two should be the same data type not different ones, String does not = int.
Something like:

String word=;

Also in line 15 you are calling the .length() method and forgot the ().
Move your Scanner object that you create in the program into the main method or declare it static.

Looks like stevanity, just beat me!

public class Strrings {
public static void main (String[] args){
	Scanner scan = new Scanner (;
     String str="java";
     System.out.println(str+" - has length  "+str.length());

it is count number of letter
but if i have space length is count space as letter and this is not true
how i can count number of leter without space ...

Member Avatar

Using a loop, scan through each character of your string, testing for spaces.
If you are looking for counts of each letter, create a array to hold your counts and increment the array for THAT letter.
Hint: You may want to ignore the case of the letter.

A better solution would be to replace all " " with "". Then find stringlength

Like this:

String input="He is a boy";
Integer length=input.replace(" ","").length();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.