Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Convert array string into integer
       
Author Reply
Maha
posted 816 posts
since Aug 13, 2006 
from

Convert array string into integer

  Posted on: 22 Feb 2012       
This program has to finds whether a book dealer carries a requested book. When using BinarySearch() method both argument inside the parenthesis should be in integer.

How to convert string array ("books") into integer array.


using System;
public class DebugSix04
{
public static void Main()
{
String[] books = { "Rich Dad", "Poor Dad", "Catch-22", "Harry Potter", "Programming Using C#", "Wizard of Oz", "The Deep" };

Console.Write("What book are you looking for? ");

int enterBookName = Convert.ToInt32(Console.ReadLine());

int x = Array.BinarySearch(books, enterBookName);

if (x > 0)
Console.WriteLine("{0} not found", enterBookName);
else
Console.WriteLine("Yes, we carry {0}", enterBookName);
}
}

Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Convert array string into integer
  Posted on: 22 Feb 2012   Accepted Answer     0  
It's not in fact the case that any of the parameters to Array.BinarySearch need to be an integer (at least for the overloads which are  normally used) though the return value is always an integer which represents the index of the item if found. If the item is not found then the method returns a negative integer.

Also the array needs to be sorted first before this method can be used.

So I think you can do it like this - I've made the search case insensitive which would be normal when looking for book titles:

using System;
using System.Collections; //needed to do case insensitive search

public class DebugSix04
{
   public static void Main()
   {
     String[] books = { "Rich Dad", "Poor Dad", "Catch-22", "Harry Potter", "Programming Using C#", "Wizard of Oz", "The Deep" };
     IComparer comparer = new CaseInsensitiveComparer();
     Array.Sort(books, comparer);

     Console.Write("What book are you looking for? ");

     string enterBookName = Console.ReadLine();

     int x = Array.BinarySearch(books, enterBookName, comparer);

     if (x < 0)
         Console.WriteLine("{0} not found", enterBookName);
     else
         Console.WriteLine("Yes, we carry {0}", books[x]);
   }
}

Maha
posted  816 posts
since  Aug 13, 2006 
from 

 Re: Convert array string into integer
  Posted on: 22 Feb 2012        0  
Thank you for your help.

I wish to know whether both arguments inside the parenthesis of BinarySearch() method can be different types. That means for example:

BinarySearch(int, string)

Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Convert array string into integer
  Posted on: 22 Feb 2012        0  
Here's a list of the overloads of the Array.BinarySearch method:

http://msdn.microsoft.com/en-us/library/system.array.binarysearch.aspx 

As you'll see, there isn't one which takes an int as its first parameter.

In fact all of them take the array itself as the first parameter. This is necessary because it's a static method of the Array class.

The first and second parameters are always of different types.
Maha
posted  816 posts
since  Aug 13, 2006 
from 

 Re: Convert array string into integer
  Posted on: 22 Feb 2012        0  
That means any type of array is considered only as an array (not the type it belongs to) inside the parenthesis of BinarySearch() method. I wish to know whether my understanding is correct.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Convert array string into integer
  Posted on: 23 Feb 2012        0  
Well, if you study the overloads, there are two types:

1. Those which take an Array as the first parameter.

2. Those which take a T[] as the first parameter where T is a generic type parameter.

The first group are weakly typed and are not bothered at compile time what the type of the array is. Consequently, if you try to search for an element which is not compatible  with the type of the array, you will get an exception.

The second group are strongly typed and the compiler infers the type of T (and hence the type of element to look for) from the type of the array that is being passed to it. The compiler will choose these overloads, rather than the others, when it can. 
Maha
posted  816 posts
since  Aug 13, 2006 
from 

 Re: Convert array string into integer
  Posted on: 23 Feb 2012        0  
Thank you for your explanation.

Could you explain what is meant by "This is necessary because it's a static method of the Array class".

This program has been altered to take two different types in BinarySearch() method. Please use T[] and correct it.

using System;
namespace BinarySearchDemo
{
class Program
{
static void Main(string[] args)
{
int[] zip = new int[5] { 1000, 2000, 3000, 4000, 5000 };

Console.Write("Entre a ZIP code ");
string enter = Console.ReadLine();

int num = Array.BinarySearch(zip, enter);

if (num < 0)
Console.WriteLine("Zip is not found");
else
Console.WriteLine("Array position is {0}", num);

Console.ReadKey();
}
}
}

Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Convert array string into integer
  Posted on: 23 Feb 2012        0  
Well, a static method of the Array class has no instance associated with it. You therefore have to pass it one as an argument.

I've altered the code so that the compiler will now choose the Array.BinarySearch<T>(T[], T) overload where T is 'int'.

Notice that it's not necessary to specify the generic type parameter <int> here - it will still work fine if you omit it. This is because the compiler will infer the type to be int as zip is an int array and zipcode is a scalar int variable:

using System;

namespace BinarySearchDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] zip = new int[5] { 1000, 2000, 3000, 4000, 5000 };

       Console.Write("Enter a ZIP code ");
       string enter = Console.ReadLine();
       int zipCode;
       int.TryParse(enter, out zipCode); // zip will be 0 if enter is invalid 
       int num = Array.BinarySearch<int>(zip, zipCode); // <int> is optional here

       if (num < 0)
          Console.WriteLine("Zip is not found");
       else
          Console.WriteLine("Array position is {0}", num);

       Console.ReadKey();
    }
  }
}
Maha
posted  816 posts
since  Aug 13, 2006 
from 

 Re: Convert array string into integer
  Posted on: 23 Feb 2012        0  
Thank you for your help.
       
Nevron Gauge for SharePoint
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved