Must Qualify the allocation with an inclosing instace of type Count

(e.g. x.new A() where x is an instance of Count)

Page 1 of 1

13 Replies - 1583 Views - Last Post: 30 March 2009 - 11:05 AM Rate Topic: -----

#1 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 05:22 PM

I'm currently working on a class project where I need to take user inputed string and count how many vowels and/or consonants are in it. The line that I am getting the error in is:
VowelConsonants vc = new VowelConsonants(inputString);
in the main.
Thank you for any help you can give me,
Barb

package challenge4;

public class Count 
{
	public class VowelConsonants {
		 
		private char[] vowels;
		private char[] consonants;
		private int numVowels = 0;
		private int numCons = 0;
		String inputString;
	
		public VowelConsonants(String str) {	   //constructor for string object
			   inputString = str;
		}
		public int getTotal()
		{
			return inputString.length();
		}
	 
		public int getNumVowels() {		 //method to retrieve vowel count
			return numVowels;
		}
	 
		public int getNumConsonants() {	  //method to retrieve consonant count
			return numCons;
		}

	
	public void countVowelsandConsonants()	  //method to count the vowels and consonants
	{
	   for(int i=0;i<total();i++)	 
	   {
		 char c = inputString.charAt(i);
		  if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
		  {
			  numVowels++;
		  }
		  else if(Character.isLetter(c))
		  {
			  numCons++;
		  }
	   } }
	  private int total() {
		
		return total();
	}}}

package challenge4;
import java.util.Scanner;

import challenge4.Count.VowelConsonants;

public class CountTest {

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
	  String inputString;						  
	  
	  Scanner keyboard= new Scanner(System.in);
	  
	  System.out.println("Please enter a string: "); 
	  inputString= keyboard.nextLine();
	  
				 VowelConsonants vc = new VowelConsonants(inputString);
	 
 do{
	  System.out.println("Number of Vowels: " + vc.getNumVowels());
	  System.out.println("Number of Consonants: " + vc.getNumConsonants());
	  System.out.println("Total Number of Both: " + (vc.getTotal()));
	  System.out.println("Enter another String: ");
	  System.out.println("Enter e to exit!");
	  }
	while(Character.toLowerCase(0) != 'e');
}}




Is This A Good Question/Topic? 0
  • +

Replies To: Must Qualify the allocation with an inclosing instace of type Count

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 06:18 PM

Without a specific error (not just the line it's on, what the error actually says), I can only assume it's because you declared the inner class public, which is not legal. Because if it's really public...why is it nested in another class?

That's the only reason that I can see why you would be getting that error.

Edit: it's either that, or the fact that you have to create an instance of Count first, then create an instance of VowelConsenants using that Count instance.

This post has been edited by Locke: 29 March 2009 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 06:26 PM

View PostLocke, on 29 Mar, 2009 - 05:18 PM, said:

Without a specific error (not just the line it's on, what the error actually says), I can only assume it's because you declared the inner class public, which is not legal. Because if it's really public...why is it nested in another class?

That's the only reason that I can see why you would be getting that error.


Sorry, I didn't post the error with my code only in the topic line.
Error is :
No inclosing instance of type Count is accessible.
Must Qualify the allocation with an inclosing instace of type Count
didn't repost the error with the code only in my topic line(eg, x.new A() where x is an instance of Count).

Does that explain it better? I think that the argument that I am passing into new VowelConsonants(), is wrong but for the life of me I can't figure out what it is.
Barb

I'm confused by the edit you posted can you give me an example of what you mean?

This post has been edited by JavaDummie46: 29 March 2009 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 06:44 PM

No the parameter you're passing is fine, so long as it is a String variable.

What that error means is that you have to have an instance of the Count class to access the VowelConsenants class, since it's inside the Count class.

This is what I mean...

Count temp = new Count();
VowelConsenants vc = new temp.VowelConsenants(inputString);


You have to use the instance of the Count class to acces the VowelConsenants class.

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#5 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:13 PM

View PostLocke, on 29 Mar, 2009 - 05:44 PM, said:

No the parameter you're passing is fine, so long as it is a String variable.

What that error means is that you have to have an instance of the Count class to access the VowelConsenants class, since it's inside the Count class.

This is what I mean...

Count temp = new Count();
VowelConsenants vc = new temp.VowelConsenants(inputString);


You have to use the instance of the Count class to acces the VowelConsenants class.

Hope this helps! :)

Still getting the same error.
here is the code I have now, I am really getting frustrated because I can seem to get past it.

This is the class

package challenge4;

import java.util.Scanner;


public class Count 
{	
	  public static char getMenuSelection() 
	  {
			String input;  //To hold keyboard input
			char selection;  // The user's selection
	 
			//Create a Scanner object for keyboard input.
			Scanner keyboard = new Scanner(System.in);
	 
			//Display the menu.
			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.");
	 
			//Get the user's selection
			input = keyboard.nextLine();
			selection = input.charAt(0);
	 
			//Validate the input
			while (Character.toLowerCase(selection) < ' ' ||
					Character.toLowerCase(selection) > 'e') 
			{
				System.out.print("Only enter a,b,c,d or e:");
				input = keyboard.nextLine();
				selection = input.charAt(0);
			}
	 
			return selection;
		}
	

 class VowelConsonants{
	 
	  
	   public  VowelConsonants(String str) {	   //constructor for string object
		   new Count();
		inputString = str;
		}
	   
		private  int numVowels = 0;
		private  int numCons = 0;
		String inputString;
	  
		public  int getNumVowels() {		 //method to retrieve vowel count
			return numVowels;
		}
	 
		public  int getNumConsonants() {	  //method to retrieve consonant count
			return numCons;
		}

	
	public void countVowelandConsonants()	  //method to count the vowels and consonants
	{
	   int total = 0;
	for(int i=0;i<total;i++)	 
	   {
		 char c = inputString.charAt(i);
		  if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
		  {
			  numVowels++;
		  }
		  else if(Character.isLetter(c))
		  {
			  numCons++;
		  }
	   } }



And this is the main where the error is still at
package challenge4;
import java.util.Scanner;


import challenge4.Count.VowelConsonants;



public class CountTest {

	/**
	 * @param args
	 * @param 
	 * @param  
  */
	public static void main(String[] args)
	{
	  String inputString;						  
	  char selection;
	  Scanner keyboard= new Scanner(System.in);
	  
	  System.out.println("Please enter a string: "); 
	  inputString= keyboard.nextLine();
	
	  VowelConsonants vc = new VowelConsonants(inputString);
	do {
		  // Display the menu and get the user's selection.
		  selection = Count.getMenuSelection();

		  // Act on the selection
		  switch (Character.toLowerCase(selection)) {
			  case 'a':
				  System.out.println("\nNumber of Vowels: " +
						vc.getNumVowels());
				  break;
			  case 'b':
				  System.out.println("\nNumber of consonats: " +
						  vc.getNumConsonants());
				  break;
			  case 'c':
				  System.out.println("\nNumber of Vowels: " +
						  vc.getNumVowels());
				  System.out.println("Number of consonants: " +
						  vc.getNumConsonants());
				  break;
			  case 'd':
				  System.out.print("Enter a string: ");
				  inputString = keyboard.nextLine();
				 
		  }

	  } while (Character.toLowerCase(selection) != 'e');

  }

}



Please, if you can walk me thru what my problem is, I have been staring at this thing for 5 hrs.
Barb
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:39 PM

Ah, you just misunderstood where to put what I said.

Put it in the main method. ;)

public static void main(String[] args)
{
    ...

    Count c = new Count();

    VowelConsenants vc = new c.VowelConsenants(inputString);

    ...
}


You can remove the new Count(); from where you added it into the VowelConsenants constructor.

Hope this helps! :D
Was This Post Helpful? 0
  • +
  • -

#7 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:48 PM

View PostLocke, on 29 Mar, 2009 - 08:39 PM, said:

Ah, you just misunderstood where to put what I said.

Put it in the main method. ;)

public static void main(String[] args)
{
    ...

    Count c = new Count();

    VowelConsenants vc = new c.VowelConsenants(inputString);

    ...
}


You can remove the new Count(); from where you added it into the VowelConsenants constructor.

Hope this helps! :D

ok I got rid of that pesky error message but it says that c can't be resolved to a type. Nopw before I go and mess it up again can you tell me what I can do. I cheated and copied and pasted your solution in my program I hope that was ok.
Barb
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:51 PM

Hm...maybe that was my bad.

Try this -- VowelConsenants vc = new Count.VowelConsenants(inputString); instead of what I put before...-_-

This post has been edited by Locke: 29 March 2009 - 09:51 PM

Was This Post Helpful? 0
  • +
  • -

#9 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:53 PM

That took me back to the error we had before, I am working in eclipse is that makes a difference
I really appreciate this!
Barb
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:59 PM

public  VowelConsonants(String str) {//constructor for string object
		   new Count();
		inputString = str;



Doesn't new Count() need a type and a variable name?
Was This Post Helpful? 0
  • +
  • -

#11 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 09:59 PM

Its asking me to create a package C, does that mean that I need to declare challenge4.Count.VowelConsonants vc = new c.VowelConsonants(inputSTring)
Was This Post Helpful? 0
  • +
  • -

#12 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 10:05 PM

I've tried that and it still is asking me to create a package class c, fuzzyness.
I just don't know what it is wanting exactly, because I have put it the way my book tells me and the way that Locke suggested. It just seems like I must be missing something really basic and simple and I am so tired I am not seeing it.
Barb
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 29 March 2009 - 10:14 PM

have you tried making them 2 seperate classes, so its not coupling. and then do it via inheritance?
Was This Post Helpful? 0
  • +
  • -

#14 JavaDummie46  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-February 09

Re: Must Qualify the allocation with an inclosing instace of type Count

Posted 30 March 2009 - 11:05 AM

Ok, I think I have this figured out, the error I had is gone. Now the only problem I am having is it won't return a count. I have run into this before and know it is something simple but could anyone help.
This is the Class
package challenge4;

import java.util.Scanner;


public class Count 
{   public class VowelConsonants extends Count {

	VowelConsonants(String str) {
		super(str);
	
	}}
	private  int numVowels = 0;
	private  int numCons = 0;
	String inputString;
   Count(String str)//constructor for string object
   {  
	   inputString= str;
   }
	  public static char getMenuSelection() 
	  {
			String input;  //To hold keyboard input
			char selection;  // The user's selection
	 
			//Create a Scanner object for keyboard input.
			Scanner keyboard = new Scanner(System.in);
	 
			//Display the menu.
			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.");
	 
			//Get the user's selection
			input = keyboard.nextLine();
			selection = input.charAt(0);
	 
			//Validate the input
			while (Character.toLowerCase(selection) < ' ' ||
					Character.toLowerCase(selection) > 'e') 
			{
				System.out.print("Only enter a,b,c,d or e:");
				input = keyboard.nextLine();
				selection = input.charAt(0);
			}
	 
			return selection;
		}
 
		public  int getNumVowels() {		 //method to retrieve vowel count
			
			return numVowels;
		}
		public  int getNumConsonants() {	  //method to retrieve consonant count
			return numCons;
		}

	
	public void countVowelandConsonants()	  //method to count the vowels and consonants
	{
	   int total = 0;
	for(int i=0;i<total;i++)	 
	   {
		 char c = inputString.charAt(i);
		  if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
		  {
			  numVowels++;
		  }
		  else if(Character.isLetter(c))
		  {
			  numCons++;
		  }
	   } }

	  }
	 



This is the Main
package challenge4;
import java.util.Scanner;
import challenge4.Count.VowelConsonants;

public class CountTest {
	 /**
	 * @param args
	 */
	public static void main(String[] args)
	{  
	   String inputString ="";
	   Count count = new Count(inputString);
	   char selection;
	   Scanner keyboard= new Scanner(System.in);
	  
	  System.out.println("Please enter a string: "); 
	  inputString= keyboard.nextLine();
	  
	  VowelConsonants vc = count.new VowelConsonants(inputString);
	 
	do {
		  // Display the menu and get the user's selection.
		  selection = Count.getMenuSelection();

	   
		// Act on the selection
		  switch (Character.toLowerCase(selection)) {
			  case 'a':
				  System.out.println("\nTotal Vowels: " +
						vc.getNumVowels());
				  break;
			  case 'b':
				  System.out.println("\nTotal Consonants: " +
						  vc.getNumConsonants());
				  break;
			  case 'c':
				  System.out.println("\nTotal Vowels: " +vc.getNumVowels()+
						   "  Total Consonants: " +vc.getNumConsonants());
				  break;
			  case 'd':
				  System.out.print("Enter a string: ");
				  inputString = keyboard.nextLine();
				 
		  }

	  } while (Character.toLowerCase(selection) != 'e');

  }}




Any help is greatly appreciated
Barb
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1