simple char array problem that i cant seem to figure out

want to make a character array skip one when it encoounters a particul

Page 1 of 1

4 Replies - 3183 Views - Last Post: 06 June 2009 - 06:22 AM Rate Topic: -----

#1 canatan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-January 09

simple char array problem that i cant seem to figure out

Posted 05 June 2009 - 10:45 AM

basically the array readInput reads the text input into the text box called InputBox.
I'm having problems with the second if.
It displays the " twice instead of once and then skips the letter right after the "..
i dont understand where i'm going wrong..
i want it to not display the " but all the characters after the " and always skip the ending and closing "
i'm sorta using this to check whether or not there are an equal matchable number of "
this is just me checking where i go wrong.. later i'll store all the letters (not the ") into an array
 private void btnScan_Click(object sender, EventArgs e)
		{
			char[] readInput = InputBox.Text.ToCharArray();
			for (int i = 0; i < readInput.Length; i++)
			{
				char x = readInput[i];
				
				if (x==' ')
				{
					i++;
				}
				
				if (x=='"')
				{
					i++;
					while (x != '"')
					{
						MessageBox.Show(x.ToString() + "testing");
					   i++;
					}
				}MessageBox.Show(x.ToString());

			}

				
			}




		}


Is This A Good Question/Topic? 0
  • +

Replies To: simple char array problem that i cant seem to figure out

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: simple char array problem that i cant seem to figure out

Posted 05 June 2009 - 11:21 AM

View Postcanatan, on 5 Jun, 2009 - 11:45 AM, said:

basically the array readInput reads the text input into the text box called InputBox.
I'm having problems with the second if.
It displays the " twice instead of once and then skips the letter right after the "..
i dont understand where i'm going wrong..
i want it to not display the " but all the characters after the " and always skip the ending and closing "
i'm sorta using this to check whether or not there are an equal matchable number of "
this is just me checking where i go wrong.. later i'll store all the letters (not the ") into an array
 private void btnScan_Click(object sender, EventArgs e)
		{
			char[] readInput = InputBox.Text.ToCharArray();
			for (int i = 0; i < readInput.Length; i++)
			{
				char x = readInput[i];
				
				if (x==' ')
				{
					i++;
				}
				
				if (x=='"')
				{
					i++;
					while (x != '"')
					{
						MessageBox.Show(x.ToString() + "testing");
					   i++;
					}
				}MessageBox.Show(x.ToString());

			}

				
			}




		}


Your code is not quite right. Once you are in the second if statement, you are not updating the value of x, you are just incrementing i. You would want to try something like this:

do {
	i++;
	x = readInput[i];
} while (x != '"' && i < readInput.Length);
i++;



You will notice one thing. The I added i < readInput.Length to the condition. If you don't do that and there is not a second " in the string you would get an array out of bounds exception.
Was This Post Helpful? 0
  • +
  • -

#3 canatan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-January 09

Re: simple char array problem that i cant seem to figure out

Posted 05 June 2009 - 09:06 PM

okay so i tried that and it worked fine for a while but then when i combined it with other things it started skipping a letter after every else if block..
i'm trying to build a scanner using c# .. i'm attatching my source code .. please tell me where i'm going wrong with this..
and posting the same thing here too
 #region Scanner
			for (int i = 0; i < readInput.Length; i++) // beginning to read char one by one
			{
				char x = readInput[i];
				#region Whitespace
				if (char.IsWhiteSpace(x)) //checking for whitespaces
				{
					continue;
				}
				#endregion
				#region stringliteral
				else if (x == '"') //checking for " and proper" 
				{
					i++;
					//x = readInput[i];
					StringBuilder adder = new StringBuilder();
					do
					{x = readInput[i];
						MessageBox.Show(x.ToString() + "testing");
						adder.Append(x);								//continuing till next " is found
						i++;
						if (i == readInput.Length)
						{
							MessageBox.Show("WRONG");
							break;
						}
						

					} while (x != '"' && i < readInput.Length);
					whole.Add(adder);

				}
				#endregion
				#region syntax
				else if (char.IsLetter(x) || x == '_' && x!='"') // checking for syntax
				{
					StringBuilder adder = new StringBuilder();
					do
					{
						x = readInput[i];
						MessageBox.Show(x.ToString() + "Letter Only");
						adder.Append(x);
						i++;
						if (i == readInput.Length)
						{
							break;
						}

						
					} while ((char.IsLetter(x) || x == '_' && x!='"') && i < readInput.Length);
					whole.Add(adder);
				}
				#endregion
				#region digitliteral
				else if (char.IsDigit(x)) // checking for digits
				{
					StringBuilder adder = new StringBuilder();
					do
					{x = readInput[i];
						MessageBox.Show(x.ToString() + "Is Digit");
						adder.Append(x);
						i++;
						if (i == readInput.Length)
						{
							break;
						}

						
					}
					while (char.IsDigit(x) && i < readInput.Length);
					whole.Add(adder);
				}
				#endregion
				#region comments
				else if (x == '$') //checking for comments
				{
					i++;
					x = readInput[i];
					do
					{
						MessageBox.Show(x.ToString() + "testing comments"); //continuing till next " is found
						i++;
						if (i == readInput.Length)
						{
							MessageBox.Show("WRONG");
							break;
						}
						x = readInput[i];

					} while (x != '\n' && i < readInput.Length);

				}
				#endregion
				#region operators and endline
				else
				{
					int n = i + 1;
					char y = readInput[n];
					switch (x)
					{
						case '+':
							{
								if (y == '+')
								{
									whole.Add(opend.series);
									MessageBox.Show("Series");
									MessageBox.Show(x.ToString() + y.ToString());
								}
								else
								{
									whole.Add(opend.add);
									MessageBox.Show("Plus");
									MessageBox.Show(x.ToString() + y.ToString());
								}
							}
							break;
						case '-':
							whole.Add(opend.minus);
							break;
						case '/':
							whole.Add(opend.divide);
							break;
						case '*':
							whole.Add(opend.multiply);
							break;
						case '#':
							whole.Add(opend.endline);
							break;
						case '~':
							whole.Add(opend.note);
							break;
						case '=':
							if (y == '=')
							{
								whole.Add(opend.equale);
							}
							else
								whole.Add(opend.equal);
							break;
						case '>':
						   
								if (y == '=')
									whole.Add(opend.greatere);
								else
									whole.Add(opend.greater);
							
							break;
						case '<':
							
								if (y == '=')
									whole.Add(opend.lessere);
								else
									whole.Add(opend.lesser);
							
							break;
						case '|':
							if (y == '|')
								whole.Add(opend.parallel);
							break;
						default:
							throw new Exception("Scanner Unrecognized Char" + x);
					}

				}
				#endregion


			}
			#endregion


the problem lies with the regions in the beginning but i dont know how to fix it.. its really really confusing.. please help... soon

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: simple char array problem that i cant seem to figure out

Posted 06 June 2009 - 12:43 AM

Are you building a compiler or grammar parser? Anyway, give this a try in your first else statement:
    else if (x == '"') //checking for " and proper" 
    {
        boolean isInDQuote = true;
        StringBuilder adder = new StringBuilder();
        while (isInDQuote && i < redInput.Length-1)
        {
           i++;
           x = readInput[i];
           if (x == '"')
              isInDQuote = false;
           else
              adder.Append(x);
        }
        
        if (isInDQuote)
        {
           MessageBox.Show("WRONG");
           break;
        }
        whole.Add(adder);

    }



You may be able to apply something similar to your other else statement further down in your code. I chose to do it this way because your code doesn't look like it will catch 2 double quotes in a row, i.e. "". I could be wrong, but that's what it looks like. And I think you may need to write i++; when you have 2 operators in a row so that the 2nd operator doesn't get parsed again, so for example:

    case '+':
    {
        if (y == '+')
        {
            i++; // make sure your compiler/parser doesn't parse this char again
            whole.Add(opend.series);
            MessageBox.Show("Series");
            MessageBox.Show(x.ToString() + y.ToString());
        }
        else
        {
            whole.Add(opend.add);
            MessageBox.Show("Plus");
            MessageBox.Show(x.ToString() + y.ToString());
        }
    }



I hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 canatan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-January 09

Re: simple char array problem that i cant seem to figure out

Posted 06 June 2009 - 06:22 AM

thanks u guys i fixed it ;D
thankyou masteryee for that i++ bit :D
and thanks sixofeleven for reminding me that i shud always write first then code.. and also that i shudnt give up so easy :P :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1