473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

What's wrong with my code? Counting letters in a string

I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class CountLetters
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.     Scanner input;
  8.  
  9.     input = new java.util.Scanner(System.in);
  10.  
  11.   String s;
  12.   int count = 0;
  13.  
  14.  
  15.     System.out.print( "Enter a string: " );
  16.     s = input.nextLine(); 
  17.     count = s.length();
  18.  
  19.  
  20.     System.out.println("The number of letters in the string is " +count+".");
  21.   }
  22. public static int count (String s) 
  23. {
  24.   int count = 0;
  25.   count = s.length();
  26. for(int i = 0; i < s.length(); i++) {
  27.      if(s.charAt(i) == 'i') 
  28.      {
  29.           count++;
  30.      }
  31. }}}
Mar 26 '08 #1
7 3401
JosAH
11,448 Expert 8TB
Your code doesn't even compile and your compiler can tell you exactly why it
doesn't compile; try it.

kind regards,

Jos
Mar 26 '08 #2
chaarmann
785 Expert 512MB
You are defining "count" in your main method as int.
But you also have a method called "count". This is bad practice and this is why it does not do what you expect. Always use different names!

You are assigning to your variable "count" the length of your input string (why?), but you are not calling your method "count", which would have brought back the desired number of letters "i" inside your input string.

This code does not look as if you have written it your own.
It looks as if someone has intentionally modified working code for some students to correct it. The students should understand the difference between
method and variable and between variable "i" and character "i". If they understand, they are able to correct the code.

If I am right, then helping you here may have negative influence to your learn success and wasting my time. Your teacher is paid for explaining you all things, whereas posting such code here prohibits me from helping people who really need help with their own code.

I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class CountLetters
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.     Scanner input;
  8.  
  9.     input = new java.util.Scanner(System.in);
  10.  
  11.   String s;
  12.   int count = 0;
  13.  
  14.  
  15.     System.out.print( "Enter a string: " );
  16.     s = input.nextLine(); 
  17.     count = s.length();
  18.  
  19.  
  20.     System.out.println("The number of letters in the string is " +count+".");
  21.   }
  22. public static int count (String s) 
  23. {
  24.   int count = 0;
  25.   count = s.length();
  26. for(int i = 0; i < s.length(); i++) {
  27.      if(s.charAt(i) == 'i') 
  28.      {
  29.           count++;
  30.      }
  31. }}}
Mar 26 '08 #3
I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class CountLetters
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.     Scanner input;
  8.  
  9.     input = new java.util.Scanner(System.in);
  10.  
  11.   String s;
  12.   int count = 0;
  13.  
  14.  
  15.     System.out.print( "Enter a string: " );
  16.     s = input.nextLine(); 
  17.     count = s.length();
  18.  
  19.  
  20.     System.out.println("The number of letters in the string is " +count+".");
  21.   }
  22. public static int count (String s) 
  23. {
  24.   int count = 0;
  25.   count = s.length();
  26. for(int i = 0; i < s.length(); i++) {
  27.      if(s.charAt(i) == 'i') 
  28.      {
  29.           count++;
  30.      }
  31. }}}
there is only one mistake in the count(String s) which has to return something.. If you can check it then your code runs properly
Mar 27 '08 #4
JosAH
11,448 Expert 8TB
there is only one mistake in the count(String s) which has to return something.. If you can check it then your code runs properly
And then the method returns the length of its String parameter plus the number
of times the lowercase 'i' occurs in that String. Not quite what the user wanted.

kind regards,

Jos
Mar 27 '08 #5
And then the method returns the length of its String parameter plus the number
of times the lowercase 'i' occurs in that String. Not quite what the user wanted.

kind regards,

Jos
Im sorri Jos im mistaken..Thank you for correcing me..But now actually im confused taht counting letters means in general the no. of letters present in the string or no. of times a letter is present in that string?????
Mar 27 '08 #6
Navdip
22
I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class CountLetters
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.     Scanner input;
  8.  
  9.     input = new java.util.Scanner(System.in);
  10.  
  11.   String s;
  12.   int count = 0;
  13.  
  14.  
  15.     System.out.print( "Enter a string: " );
  16.     s = input.nextLine(); 
  17.     count = s.length();
  18.  
  19.  
  20.     System.out.println("The number of letters in the string is " +count+".");
  21.   }
  22. public static int count (String s) 
  23. {
  24.   int count = 0;
  25.   for(int i = 0; i < s.length(); i++) {
  26.      if(s.charAt(i) == 'i') 
  27.      {
  28.           count++;
  29.      }
  30. }}}

Hi...
in your code in which u calculating the length of string, u were not calling your function...and also not returned calculated value.You can use the follwing code it will really help you.
---------------------------------------------------------------------------------------
I removed the spoonfeeding code; we don't do that here because the OP won't
learn anything from copying/pasting code. Don't supply code anymore.

kind regards,

Jos (moderator)
Mar 27 '08 #7
pralu
34
<Code removed by MODERATOR - Please read our Posting Guidelines.>


hope this will work for u....... n ya u r moving more inner to the scanner package.... specify it import java.util.*;
Mar 27 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
3
by: matt | last post by:
Greetings: I appoligize if I'm asking a question that's already been answered in the group - I've scoured for about two hours and I can't find a solution. I also appoligize if I over complicate...
3
by: mark | last post by:
hi guys i need hlp on a c programcounting letters,words,etc .simple. thanks
3
by: belton180 | last post by:
CODE]../* Program function: Simulate the stack using a stack limit of 10. Display a menu for the the following. Create a stack Insert an item in the stack Pop an item from the stack ...
2
by: euniceno1 | last post by:
I am just learning c language at the moment, i have problem with following program: #define TRUE 1 #define FALSE 0 #define MAX_LENGTH 100000 /* Max number of chars read from file */ #include...
0
by: raypjr | last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
8
by: fredo | last post by:
This question was asked in comp.lang.javascript with no result. In IE5.x and IE6, I want to display an image when the user rolls over a text link. The image does indeed display, but only on the...
4
by: gihan99 | last post by:
hey can somebody please explane how to count letters in a string. thxs
9
by: erictheone | last post by:
Ok so what I'm trying to do is create a trans location cipher. For those among us that don't know alot about cryptography it is a method for jumbling up letters to disguise linguistic...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.