CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    4

    [RESOLVED] Returning Memory Location

    Hello all! I am having an issue with a program I am writing for my Java class.

    The program is designed to return the number of vowels or consonants of an inputted string. My problem seems that the program returns the memory location of the array and not the actual number.

    Another problem I noticed is the counting class is being shown as being not used. However, this count is supposed to occur in the background. I am unsure of where I need to put it to call the class.

    Below is my code.

    The Main Class
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package vowelcons;
    
    import java.util.Scanner;
    
    /**
     *
     * @author Matthew Wood
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String input;
            char selection;
    
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a string: ");
            input = keyboard.nextLine();
    
            VowelCons vc = new  VowelCons(input);
    
            do{
                selection = getMenuSelection();
    
                switch(Character.toLowerCase(selection)){
                    case 'a': System.out.println("\nNumber of vowels: " + vc.getNumVowels());
                    break;
                    case 'b': System.out.println("\nNumber of consonants: " + vc.getNumConsonants());
                    break;
                    case 'c': System.out.println("\nNumber of vowels: " + vc.getNumVowels());
                              System.out.println("\nNumber of consonants: " + vc.getNumConsonants());
                    break;
                    case 'd': System.out.println("Enter a string: ");
                              input = keyboard.nextLine();
                              vc = new VowelCons(input);
                }
                }while(Character.toLowerCase(selection) != 'e');
    
            }
    
    
        public static char getMenuSelection(){
            String input;
            char selection;
    
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("a) Count the number of vowels in the string.");
            System.out.println("b) Count the number of consonants in the string.");
            System.out.println("c) Count both the vowels and consonants in the string.");
            System.out.println("d) Enter another string.");
            System.out.println("e) Exit the program.");
    
            input = keyboard.nextLine();
            selection = input.charAt(0);
    
            while(Character.toLowerCase(selection) < 'a' || Character.toLowerCase(selection) > 'e'){
                System.out.println("Only enter a,b,c,d, or e: ");
                input = keyboard.nextLine();
                selection = input.charAt(0);
            }
            return selection;
        }
    }
    The VowelsCons Class:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package vowelcons;
    
    /**
     *
     * @author Matthew Wood
     */
    public class VowelCons {
        private char [] vowels;
        private char [] consonants;
        private int numVowels = 0;
        private int numCons = 0;
        private String str;
    
        public VowelCons(String string){
                str = string;
        }
    
    
        public char[] getNumConsonants() {
            consonants = new char[]{'b','c','d','f','g','h','j','k','l',
            'm','n','p','q','r','s','t','v','w','x','y','z'};
            return consonants;
        }
    
        public char[] getNumVowels() {
            vowels = new char[]{'a','e', 'i','o','u'};
            return vowels;
        }
    
        private void countVowelsAndConsonants(int numVowels, int numCons){
            for (int i = 0; i < 100; i++) {
                char ch = str.charAt(i);
                if ((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') ) {
                    numVowels++;
                }
    
                else if (Character.isLetter(ch)) {
                    numCons++;
                }
        }
    
        }
    }
    I do know the problem has to lie in where or how my array is declared or where it is positioned. I have read my textbook and other sources and I am still feeling a bit frustrated. Thank you all for any help!

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Returning Memory Location

    problem seems that the program returns the memory location of the array and not the actual number.
    Can you show what is returned?
    It sounds like you are getting the output from the Object's default toString() method. If the problemis with your VowelCons class, add a toString method to it:
    public String toString() { ....return a nice string here }
    Another problem I noticed is the counting class is being shown as being not used
    How is that shown?
    Norm

  3. #3
    Join Date
    Aug 2010
    Posts
    4

    Re: Returning Memory Location

    Quote Originally Posted by Norm View Post


    How is that shown?
    It is shown by a gray wavy line (NetBeans 6.8). When I mouse over it, the message says "method countVowelsandCons(int numVowels, int numCons) is not used".

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Returning Memory Location

    is not used
    Is the IDE correct?
    Does it effect the desired results from your program?
    Norm

  5. #5
    Join Date
    Aug 2010
    Posts
    4

    Re: Returning Memory Location

    It does not appear to. It returns the string, "Number of Vowels is:" then it shows the memory location.

    For overriding the toString method, would I include the count method into it? Or do I make the toString method take the result from the count method?

    By the way, thank you for helping me logically step through this.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Returning Memory Location

    Put in the toString() method what you want to see returned. Its up to you.
    Norm

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Returning Memory Location

    Your code seems confused - in the VowelCons class you have numVowels and numCons int class variables that are initialized to zero and never used, and you have getNumVowels and geNumCons methods that don't return the numVowels and numCons variables, but regenerate and return the arrays every time they are called (why?).

    Until you sort out what those methods are supposed to do and give them appropriate names, and decide what those class variables are going to be used for, you're going to be confused, and people trying to help will be even more confused.

    Also, printing an array won't automatically give you a count of the items in the array (why should it?), it will just give you the default 'toString' value for an object, which is className@hashCode. If you want the number of elements in an array, use array.length. If you want to print a list of the items, iterate over the array printing each item in turn.

    And why not just initialize the arrays once, instead of regenerating them every time you call the method - the array contents never change, so it's just wasting processor time and memory.

    If you cannot describe what you are doing as a process, you don't know what you're doing...
    W. E. Deming
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    Aug 2010
    Posts
    4

    Re: Returning Memory Location

    I figured it out. I had some conflicting return types and got it sorted.

    Thank you all for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured