Jon Kosaka
Jon Kosaka

Reputation: 17

System.out.Println problems

This is written in Java.

This is a two class program (one testing) that checks a String for vowels and keeps a count of how many are present.

Everything seems to be working fine except for the System.out.println()s on the second class (for testing).

I have never come across this problem before, but I think it is only relative to the print lines themselves.

The print lines are at the bottom.

Here are the two classes:

/** Tests the sentence for the vowels
 * @ Jon Kosaka
 */

public class Sentence
{
    String sentence;

    public Sentence(String text)
    {
        sentence = text;
    }

    public boolean isPalindrome()
    {

        int low = 0;

        int high = sentence.length() -1;

        boolean match = true;

        while (low < high && match)
        {
            if (sentence.charAt(low) != sentence.charAt(high))
            {
                match = false;
            }

            low++;
            high--;
        }
        return match;
    }

    public String getSentence()
    {
        return sentence;
    }

    public void setSentence(String newText)
    {
        sentence = newText;
    }


    public int[] getVowelCounts()
    {

        final int A_INDEX = 0;
        final int E_INDEX = 0;
        final int I_INDEX = 0;
        final int O_INDEX = 0;
        final int U_INDEX = 0; 

        //              a  e  i  o  u
        int[] counts = {A_INDEX, E_INDEX, I_INDEX, O_INDEX, U_INDEX};
        int i;
        for (i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(0) == 'a')
                counts[0]++;
            if (sentence.charAt(0) == 'e')
                counts[1]++;
            if (sentence.charAt(0) == 'i')
                counts[2]++;
            if (sentence.charAt(0) == 'o')
                counts[3]++;
            if (sentence.charAt(0)  == 'u')
                counts[4]++;

        }
        return counts;
    }


}

Other class:

import java.util.Scanner;
/**
  An application to count the number of each individual vowel
  @ Jon Kosaka
 */

public class WorkingWithStrings
{

    public static void main(String[] args)
    {
        //create a scanner
        Scanner console = new Scanner(System.in);

        //get the input
        System.out.println("Enter a sentence and I will count each vowel: ");
        String input = console.nextLine();

        //Create a vowelCounter and call method to count the vowels
        Sentence sentence = new Sentence(input);
        int[] counts = sentence.getVowelCounts();
    }
    //display the results
    System.out.println("Vowel Count for the sentence: ");
    System.out.println("      " + sentence.getSentence());
    System.out.println("number of a: " + counts[Sentence.A_INDEX]);
    System.out.println("number of e: " + counts[Sentence.E_INDEX]);
    System.out.println("number of i: " + counts[Sentence.I_INDEX]);
    System.out.println("number of o: " + counts[Sentence.O_INDEX]);
    System.out.println("number of u: " + counts[Sentence.U_INDEX]);

}

Upvotes: 0

Views: 1739

Answers (6)

Abid Hussain
Abid Hussain

Reputation: 9

there are few issues in your code i think this one is exactly right/correct code that you want.

 public class Sentence
{
   String sentence;

   public Sentence(String text)
   {
      sentence = text;
   }

   public boolean isPalindrome()
   {

      int low = 0;

      int high = sentence.length() -1;

      boolean match = true;

       while (low < high && match)
       {
          if (sentence.charAt(low) != sentence.charAt(high))
          {
             match = false;
          }

          low++;
          high--;
       }
       return match;
   }

   public String getSentence()
   {
      return sentence;
   }

   public void setSentence(String newText)
   {
      sentence = newText;
   }


   public int[] getVowelCounts()
   {

       final int A_INDEX = 0;
       final int E_INDEX = 0;
       final int I_INDEX = 0;
       final int O_INDEX = 0;
       final int U_INDEX = 0; 

//              a  e  i  o  u
int[] counts = {A_INDEX, E_INDEX, I_INDEX, O_INDEX, U_INDEX};
int i;
for (i = 0; i < sentence.length(); i++) {
 if (sentence.charAt(i) == 'a')
     counts[0]++;
 if (sentence.charAt(i) == 'e')
     counts[1]++;
 if (sentence.charAt(i) == 'i')
     counts[2]++;
 if (sentence.charAt(i) == 'o')
     counts[3]++;
 if (sentence.charAt(i)  == 'u')
     counts[4]++;

}
return counts;
   }


}




import java.util.Scanner;
/**
   An application to count the number of each individual vowel
   @ Jon Kosaka
*/

public class WorkingWithStrings
{

   public static void main(String[] args)
   {
      //create a scanner
      Scanner console = new Scanner(System.in);

      //get the input
      System.out.println("Enter a sentence and I will count each vowel: ");
      String input = console.nextLine();

      //Create a vowelCounter and call method to count the vowels
      Sentence sentence = new Sentence(input);
      int[] counts = sentence.getVowelCounts();

//display the results

      System.out.println("Vowel Count for the sentence: ");
      System.out.println("      " + sentence.getSentence());
      System.out.println("number of a: " + counts[0]);
      System.out.println("number of e: " + counts[1]);
      System.out.println("number of i: " + counts[2]);
      System.out.println("number of o: " + counts[3]);
      System.out.println("number of u: " + counts[4]);

  }

}

Upvotes: 0

codaddict
codaddict

Reputation: 455410

There are many bugs :)

1:

To find and increment vowel counts you have:

for (i = 0; i < sentence.length(); i++) {
 if (sentence.charAt(0) == 'a')
     counts[0]++;
 ...

which checks the character at index 0 in every iteration. So

if (sentence.charAt(0) == 'a')

should be

if (sentence.charAt(i) == 'a')

2:

You've misplaced a } in your main method, as a result some of the code is outside any function. Fix that.

3:

The constants A_INDEX, B_INDEX... are method local in one class and you are accessing them from another class. You need to make them static variables of the Sentence class. Also you've assigned all of them value 0, Since they are used as the index in the array which hold vowel count. so you need:

static final int A_INDEX = 0;
static final int E_INDEX = 1;
static final int I_INDEX = 2;
static final int O_INDEX = 3;
static final int U_INDEX = 4; 

4:

Change:

int[] counts = {A_INDEX, E_INDEX, I_INDEX, O_INDEX, U_INDEX};

to

int[] counts = {0,0,0,0,0};

Here is a working program

Upvotes: 2

Thomas
Thomas

Reputation: 88757

Without knowing the exact problem, here are some suspicions:

counts[Sentence.A_INDEX] etc. references a static field A_INDEX which doesn't exist in Sentence. If it would exist as a static variable, all of those would still be initialized to 0 so counts[Sentence.A_INDEX] and counts[Sentence.E_INDEX] would both be translated to counts[0].

Upvotes: 0

hungneox
hungneox

Reputation: 9839

You have to put System.out.print in public static void main

public class WorkingWithStrings {

   public static void main(String[] args)
   {
      //create a scanner
      Scanner console = new Scanner(System.in);

      //get the input
      System.out.println("Enter a sentence and I will count each vowel: ");
      String input = console.nextLine();

      //Create a vowelCounter and call method to count the vowels
      Sentence sentence = new Sentence(input);
      int[] counts = sentence.getVowelCounts();

//display the results
      System.out.println("Vowel Count for the sentence: ");
      System.out.println("      " + sentence.getSentence());
      System.out.println("number of a: " + counts[Sentence.A_INDEX]);
      System.out.println("number of e: " + counts[Sentence.E_INDEX]);
      System.out.println("number of i: " + counts[Sentence.I_INDEX]);
      System.out.println("number of o: " + counts[Sentence.O_INDEX]);
      System.out.println("number of u: " + counts[Sentence.U_INDEX]);
  }


}

Upvotes: 1

Danalog
Danalog

Reputation: 559

I see two problems with this:

  1. Your System.out.println() is sitting outside your main function
  2. Sentence.A_INDEX, etc, aren't static class variables, they're declared inside a member function

Upvotes: 1

Udo Held
Udo Held

Reputation: 12548

Some of your statements are outside of any method. This won't compile.

import java.util.Scanner;
/**
   An application to count the number of each individual vowel
   @ Jon Kosaka
*/

public class WorkingWithStrings
{

  public static void main(String[] args)
  {
    //create a scanner
    Scanner console = new Scanner(System.in);

    //get the input
    System.out.println("Enter a sentence and I will count each vowel: ");
    String input = console.nextLine();

    //Create a vowelCounter and call method to count the vowels
    Sentence sentence = new Sentence(input);
    int[] counts = sentence.getVowelCounts();
    //}  DELETED HERE
    //display the results
    System.out.println("Vowel Count for the sentence: ");
    System.out.println("      " + sentence.getSentence());
    System.out.println("number of a: " + counts[Sentence.A_INDEX]);
    System.out.println("number of e: " + counts[Sentence.E_INDEX]);
    System.out.println("number of i: " + counts[Sentence.I_INDEX]);
    System.out.println("number of o: " + counts[Sentence.O_INDEX]);
    System.out.println("number of u: " + counts[Sentence.U_INDEX]);
  } //ADDED HERE
}

Upvotes: 2

Related Questions