Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Help in Converting C# to Java (http://www.programmingforums.org/showthread.php?t=14932)

bearvsgodzila Jan 13th, 2008 2:27 AM

Help in Converting C# to Java
 
Hey guys, need some help converting c# codes to java.

there's a lot of errors(repetitive at each else-if) and i only spot the starting of the codes, can any one help? :'(

i don't know what's "Convert.ToInt32", the char numbers can't seem to convert into toString. sorry for my bad english :)
:


 import java.*;
  import java.math.*;
  import java.util.*;

    class NricCheck {
 
     
 
 
      public static void main (String [] args){
     
       

      }
     
      public boolean nricValidator(String nric){
     
      char[] nricArray = nric.toCharArray();
            int sum = 0;
            int num = 0;
            int result = 0;
            char numbers;
            String no = "";
            boolean valided = false;
                               

            try
            {
                for (int i = 0; i<nricArray.length; i++)
                {
                    if (i == 1)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.toString();
                        num = Convert.ToInt32(no);
                        num *= 2;
                        nricArray[i] = Convert.ToChar(num);
                    }
                    else if (i == 2)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.ToString();
                        num = Convert.ToInt32(no);
                        num *= 7;
                        nricArray[i] = Convert.ToChar(num);
                    }
                    else if (i == 3)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.ToString();
                        num = Convert.ToInt32(no);
                        num *= 6;
                        nricArray[i] = Convert.ToChar(num);
                    }
                    else if (i == 4)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.ToString();
                        num = Convert.ToInt32(no);
                        num *= 5;
                        nricArray[i] = Convert.ToChar(num);
                    }
                    else if (i == 5)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.ToString();
                        num = Convert.ToInt32(no);
                        num *= 4;
                        nricArray[i] = Convert.ToChar(num);
                    }
                    else if (i == 6)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.ToString();
                        num = Convert.ToInt32(no);
                        num *= 3;
                        nricArray[i] = Convert.ToChar(num);
                    }
                    else if (i == 7)
                    {
                        num = 0;
                        numbers = nricArray[i];
                        no = numbers.ToString();
                        num = Convert.ToInt32(no);
                        num *= 2;
                        nricArray[i] = Convert.ToChar(num);
                    }
                }

                for (int i = 0; i < nricArray.Length; i++)
                {
                    if (i > 0 && i < 8)
                    {
                        numbers = nricArray[i];

                        num = Convert.ToInt32(numbers);

                        sum += num;
                    }
                }

                result = sum % 11;

                if (result == 10)
                {
                    if (nricArray[8] == 'A' || nricArray[8] == 'a')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'A'", "Nric Error");
                    }
                }
                else if (result == 9)
                {
                    if (nricArray[8] == 'B' || nricArray[8] == 'b')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'B'", "Nric Error");
                    }
                }
                else if (result == 8)
                {
                    if (nricArray[8] == 'C' || nricArray[8] == 'c')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'C'", "Nric Error");
                    }
                }
                else if (result == 7)
                {
                    if (nricArray[8] == 'D' || nricArray[8] == 'd')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                      System.out.println.out("Your last alphabet should be 'D'", "Nric Error");
                    }
                }
                else if (result == 6)
                {
                    if (nricArray[8] == 'E' || nricArray[8] == 'e')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'E'", "Nric Error");
                    }
                }
                else if (result == 5)
                {
                    if (nricArray[8] == 'F' || nricArray[8] == 'f')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'F'", "Nric Error");
                    }
                }
                else if (result == 4)
                {
                    if (nricArray[8] == 'G' || nricArray[8] == 'g')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'G'", "Nric Error");
                    }
                }
                else if (result == 3)
                {
                    if (nricArray[8] == 'H' || nricArray[8] == 'h')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'H'", "Nric Error");
                    }
                }
                else if (result == 2)
                {
                    if (nricArray[8] == 'I' || nricArray[8] == 'i')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'I'", "Nric Error");
                    }
                }
                else if (result == 1)
                {
                    if (nricArray[8] == 'Z' || nricArray[8] == 'z')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'Z'", "Nric Error");
                    }
                }
                else if (result == 0)
                {
                    if (nricArray[8] == 'J' || nricArray[8] == 'j')
                    {
                        valided = true;
                    }
                    else
                    {
                        valided = false;
                        System.out.println.out("Your last alphabet should be 'J'", "Nric Error");
                    }
                }
            }
            catch (FormatException e)
            {
                System.out.println("Check your NRIC. You may have key it wrongly.", "Error");
            }
            catch (IndexOutOfRangeException e)
            {
                System.out.println("Check your NRIC. You may have key it wrongly.", "Error");
            }
            catch (NullReferenceException e)
            {
                System.out.printl("Check your NRIC. You may have key it wrongly.", "Error");
            }

          return nric;
        }
     
     
      }


ReggaetonKing Jan 13th, 2008 11:36 AM

Re: Help in Converting C# to Java
 
Convert.ToInt32 is Integer.parseInt(String val) in Java. The type 'char' is not a struct as it is in C# so I would just convert a character to a string by doing:
:

char c  = 'A';
String str = c + "";



All times are GMT -5. The time now is 4:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC