Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Programming Finding a character

  • 27-05-2010 04:22PM
    #1
    Closed Accounts Posts: 68 ✭✭


    hi i am trying to find how i can print the number of times the character 'e' appears in your name. can anybody help me with this. :confused:

    lets say my name is gerry byrne erin

    hope this make sense to you,


Comments

  • Registered Users, Registered Users 2 Posts: 428 ✭✭Joneser


    I would say add the string to a charArray and iterate through it, increasing the count by 1 every time a char = e, or the relevant ascii value, "lower case e has an ascii value of 101)

    I can give some help with pseudo code if u like, but is prob better if u give it a go yourself first


  • Closed Accounts Posts: 68 ✭✭Gman_Ireland


    @Joneser if you could provide the code that would be great, have tried and will look at it again. thanks


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    There are a few ways to do this.

    One is as mentioned, iterating through the array.

    Let's say you have a string called name and it's value is "teddy".

    You could do the following
    int characterCount = 0;
    String search = userinput(); // use whatever scanner / i/o class you want
    for(int i = 0; i < name.length(); i++){
     if(name.charAt(i)).equals(search)) {
      characterCount++;
      }
    }
    
    System.out.println("The character: " + search + " has been found: " + characterCount + " times.");
    
    

    That's it more or less I'd imagine.


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    @Joneser if you could provide the code that would be great, have tried and will look at it again. thanks

    http://www.boards.ie/vbulletin/showpost.php?p=52731901&postcount=2


  • Closed Accounts Posts: 68 ✭✭Gman_Ireland


    @Sparks thanks
    here is what i have done so far,
    : 
    
    import java.util.Scanner;
    
    class  week5_scanner {
    
    		public static void main(String args[]){
    		
    			String name;
    			Scanner in = new Scanner(System.in);
    			
    			System.out.println("What is your name" );
    			name = in.nextLine();
    			char c1 = name.charAt(0);
    			int pos = name.indexOf(" ");
    			char c2 = name.charAt(pos + 1);
    			in.close();
    			
    			String lowername= name.toLowerCase();
    			System.out.println("Hello " + lowername );
    			
    			String uppername= name.toUpperCase();
    			System.out.println("Hello " + uppername );
    			
    			int len = name.length();
    			System.out.println("STR LEN = " + name.length() );
    			
    			System.out.println("My initials are :- " + c1 + c2);
    			
    			char at = name.charAt(0); 
    			System.out.println("first char is = " + name.charAt(0));
    			
    		}
    		
    		}
    


  • Advertisement
  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Sorry, forgot to convert the character to a string. Here's full working code for you.
    public class countChar{
    	public static void main(String[] args){
    		int count = 0;
    		String name = "Teddy"; 
    		String search = "d"; // replace this with user input
    		String currentChar;
    
    		for(int i=0; i<name.length(); i++){
    			currentChar = Character.toString(name.charAt(i));
    			if(currentChar.equals(search)){
    				count++;
    			}
    		}
    
    		System.out.println("The character: " + search + " has been found: " + count + " times.");
    
    
    	}
    }
    


  • Closed Accounts Posts: 68 ✭✭Gman_Ireland


    dlofnep wrote: »
    Sorry, forgot to convert the character to a string. Here's full working code for you.
    public class countChar{
    	public static void main(String[] args){
    		int count = 0;
    		String name = "Teddy"; 
    		String search = "d"; // replace this with user input
    		String currentChar;
    
    		for(int i=0; i<name.length(); i++){
    			currentChar = Character.toString(name.charAt(i));
    			if(currentChar.equals(search)){
    				count++;
    			}
    		}
    
    		System.out.println("The character: " + search + " has been found: " + count + " times.");
    
    
    	}
    }
    

    @dlofnep Thanks that helps alot.


  • Closed Accounts Posts: 68 ✭✭Gman_Ireland


    @dlofnep Thanks got it and have it in, really appreciate the help.

    :):):) how do you put the code in the way you did on boards.
    import java.util.Scanner;
    
    class  week5_scanner {
    
    		public static void main(String args[]){
    		
    			String name;
    			Scanner in = new Scanner(System.in);
    				
    
    
    			System.out.println("What is your name" );
    			name = in.nextLine();
    			char c1 = name.charAt(0);
    			int pos = name.indexOf(" ");
    			char c2 = name.charAt(pos + 1);
    			int count = 0;
    			String search = "e";
    			String currentChar; 
    			in.close();
    			
    			String lowername= name.toLowerCase();
    			System.out.println("Hello " + lowername );
    			
    			int len = name.length();
    			System.out.println("STR LEN = " + name.length() );
    			
    			System.out.println("My initials are :- " + c1 + c2);
    			
    			char at = name.charAt(0); 
    			System.out.println("first char is = " + name.charAt(0));
    			
    			for(int i=0; i<name.length(); i++){
    			currentChar = Character.toString(name.charAt(i));
    			if(currentChar.equals(search)){
    				count++;
    			}
    		}
    			System.out.println("The character: " + search + " has been found: " + count +  " times.");
    			
    			 
    					
    	} 
    }
    


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    No probs :)

    You place it within the CODE tags. If you have any questions about the code, just ask and I'll explain it all to you.


  • Registered Users, Registered Users 2 Posts: 3,766 ✭✭✭Reku


    dlofnep wrote: »
    Sorry, forgot to convert the character to a string. Here's full working code for you.
    public class countChar{
    	public static void main(String[] args){
    		int count = 0;
    		String name = "Teddy"; 
    		String search = "d"; // replace this with user input
    		String currentChar;
    
    		for(int i=0; i<name.length(); i++){
    			currentChar = Character.toString(name.charAt(i));
    			if(currentChar.equals(search)){
    				count++;
    			}
    		}
    
    		System.out.println("The character: " + search + " has been found: " + count + " times.");
    
    
    	}
    }
    

    Why convert the character you wish to search for and the character at index i to strings? Are you just trying to make it easy to extend it to searching for actual strings (e.g. just switch the charAt function for a subString one)?
    char currentChar = 'd'; //whatever character you wish to search for.
    
    for(int i=0; i<name.length(); i++)
    {
    	if(currentChar == name.charAt(i))
            {
    		count++;
    	}
    }
    


  • Advertisement
  • Closed Accounts Posts: 68 ✭✭Gman_Ireland


    @dlofnep
    thanks for that, 
    
    
                                   think i got it now. 
    
    
    defo learning something everyday this week. 
    
    
    
    


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Reku wrote: »
    Why convert the character you wish to search for and the character at index i to strings? Are you just trying to make it easy to extend it to searching for actual strings (e.g. just switch the charAt function for a subString one)?

    .equals requires the character to be a string. I just figured that charAt() would be more intuitive than substring() for the OP to read. No other reason tbh. But yes, either way would work.


Advertisement