excetion with array

Can't get array to work with try catch statement

Page 1 of 1

4 Replies - 773 Views - Last Post: 18 May 2009 - 12:25 PM Rate Topic: -----

#1 CCONNER9582002  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 21-April 09

excetion with array

Post icon  Posted 18 May 2009 - 11:21 AM

I have tried several different ways and can't get a try statement to work with an array. The book which I am referencing doesn't have an example with an array, but does have an example with the number. I got the number to work but seem to be runing into an issue when I try to use the same example with an array. Here is what I have at the moment.


using System;

public class flavors
{  

	public static void Main ()
	{
		string Icecream;

		GetFlavor sp = new GetFlavor();
		bool isFlavorGood = false;
		while (!isFlavorGood)
		{
			try
			{
				sp.promptForFlavor();
				isFlavorGood = true;
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.Writeline("These are the flavors to pick from.");
			}
		}
	}


	public class GetFlavor
	{
		private string[] flavor = {"strawberry", "chocolate"};
		public void promtForFlavor()
		{
			stringstrIcecream;
			try
			{
				Console.WriteLine("What Flavor would you like?");
				strIcecream = Console.ReadLine();
			
				//Check if flavor is equal to the string
				if (strIcecream != flavor)
				{
					Console.WriteLine("We don't have that Flavor"};
				strIcecream = Console.ReadLine();
			}
			//else
			//{
			//	Console.WriteLine("Enjoy your icecream.");
			//}
		}
			catch(FormatException fe);
			{

				throw(fe);
			}
			}						
	}
}



Could someone guide me to a website were I can get an example of an exception which uses an array. Without an example I am having a hard time figureing out what statement I am missing in order to correct this issue.

Is This A Good Question/Topic? 0
  • +

Replies To: excetion with array

#2 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: excetion with array

Posted 18 May 2009 - 11:30 AM

  if (strIcecream != flavor)
				{
					Console.WriteLine("We don't have that Flavor"};
				strIcecream = Console.ReadLine();
			}


here is your problem is here in this line mate the problem is that flavor is an array and you cant read an array directly for this i will suggest you to put a for loop arround it like this may be
for(int i=0;i<strcIcecrea.length;i++){
  if (strIcecream != flavor[i])
				{
					Console.WriteLine("We don't have that Flavor"};
				strIcecream = Console.ReadLine();
			}
... your else loop 



and here also
stringstrIcecream;


to
string strIcecream;


i think you got it now mate ya you can use indexoutofrangeException but that will only point you if you are reading or adding a value thats outside the size of an array but with the use of length property of the array i dont think so that should be problem

ya one more thing if u use that flavor variable directly it will display sometihng like this system.string not the actual value

This post has been edited by prajayshetty: 18 May 2009 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 bigtonyk  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 61
  • Joined: 17-May 09

Re: excetion with array

Posted 18 May 2009 - 12:02 PM

View PostCCONNER9582002, on 18 May, 2009 - 10:21 AM, said:

I have tried several different ways and can't get a try statement to work with an array. The book which I am referencing doesn't have an example with an array, but does have an example with the number. I got the number to work but seem to be runing into an issue when I try to use the same example with an array. Here is what I have at the moment.


using System;

public class flavors
{  

	public static void Main ()
	{
		string Icecream;

		GetFlavor sp = new GetFlavor();
		bool isFlavorGood = false;
		while (!isFlavorGood)
		{
			try
			{
				sp.promptForFlavor();
				isFlavorGood = true;
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.Writeline("These are the flavors to pick from.");
			}
		}
	}


	public class GetFlavor
	{
		private string[] flavor = {"strawberry", "chocolate"};
		public void promtForFlavor()
		{
			stringstrIcecream;
			try
			{
				Console.WriteLine("What Flavor would you like?");
				strIcecream = Console.ReadLine();
			
				//Check if flavor is equal to the string
				if (strIcecream != flavor)
				{
					Console.WriteLine("We don't have that Flavor"};
				strIcecream = Console.ReadLine();
			}
			//else
			//{
			//	Console.WriteLine("Enjoy your icecream.");
			//}
		}
			catch(FormatException fe);
			{

				throw(fe);
			}
			}						
	}
}



Could someone guide me to a website were I can get an example of an exception which uses an array. Without an example I am having a hard time figureing out what statement I am missing in order to correct this issue.


you have a few issues.

stringstrIcecream;

is probably a typo.
it should be :
string strIcecream;

try
			{
				Console.WriteLine("What Flavor would you like?");
				strIcecream = Console.ReadLine();
			
				//Check if flavor is equal to the string
				if (strIcecream != flavor)
				{
					Console.WriteLine("We don't have that Flavor"};
				strIcecream = Console.ReadLine();
			}
			//else
			//{
			//	Console.WriteLine("Enjoy your icecream.");
			//}
		}
			catch(FormatException fe);
			{

				throw(fe);
			}



in the line if (strIcecream != flavor)
you are trying to compare a string to an array and that's not ever going to be true. I'd write a function that returns a bool that checks for valid flavors


private bool isValid(string myFlavor)
{
   bool success = false;
	foreach (string s in flavor)
	   {
		  if (s.Equals(myFlavor) success = true;
	   }
	return success;
}



and your catch() statement is ONLY going to catch FormatExceptions. You should use a generic catch

catch (Exception ex)
{
// do something here like return false.
}

all you are doing is throwing the same exception that's already been thrown.

so code might look something like


public class GetFlavor
	{
		private string[] flavor = {"strawberry", "chocolate"};
		public void promtForFlavor()
		{
			string strIcecream;
			try
			{
					Console.WriteLine("What Flavor would you like?");
					strIcecream = Console.ReadLine();
				
					//Check if flavor is equal to the string
					if (!isValid(strIcecream))
					{
						Console.WriteLine("We don't have that Flavor");
						strIcecream = Console.ReadLine();
					}
				else
				{
					Console.WriteLine("Enjoy your icecream.");
				}
			}
			catch(Exception fe);
			{
				Console.WriteLine("Something bad happened. " + ex.Message);
			}
	}	

	private bool isValid(string myFlavor)
	{
	   bool success = false;
		foreach (string s in flavor)
		   {
			  if (s.Equals(myFlavor) success = true;
		   }
		return success;
	}				
}


Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: excetion with array

Posted 18 May 2009 - 12:13 PM

it will give the same result as above code of mine same explanation :) but an alternate way

This post has been edited by prajayshetty: 18 May 2009 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 bigtonyk  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 61
  • Joined: 17-May 09

Re: excetion with array

Posted 18 May 2009 - 12:25 PM

View Postprajayshetty, on 18 May, 2009 - 11:13 AM, said:

it will give the same result as above code of mine same explanation :) but an alternate way


No it won't.


for(int i=0;i<strcIcecrea.length;i++){
  if (strIcecream != flavor[i])
				{
					Console.WriteLine("We don't have that Flavor"};
				strIcecream = Console.ReadLine();
			}
... your else loop 



is wrong unless the users selects the first flavor in the array. Otherwise,
it'll always give them the "We don't have that Flavor" message.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1