using split within an array
Page 1 of 110 Replies - 4445 Views - Last Post: 12 May 2010 - 10:15 PM
#1
using split within an array
Posted 12 May 2010 - 05:25 PM
Replies To: using split within an array
#2
Re: using split within an array
Posted 12 May 2010 - 06:58 PM
say...
string text = "one,two,three,four,five";
string[] words = text.Split(','); // creates an array containing ["one", "two", "three", "four", "five"]
// by splitting the string using a comma to separate it(this could be
// any character though
foreach (string s in words)
{
Console.WriteLine(s);
}
#3
Re: using split within an array
Posted 12 May 2010 - 07:00 PM
Here's an example on using it to split on the spaces in a sentence
private void SplitExample()
{
string example = "This is an example on using the split method";
string[] words = example.Split(new char[]{' '});
foreach(string word in words)
{
Console.WriteLine(word);
}
}
#4
Re: using split within an array
Posted 12 May 2010 - 07:47 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project_2_2
{
class Program
{
static void Main()
{
double num1;
double num2;
double total;
string [] equation = { num1 + num2, num1 - num2, num1 * num2,num1 / num2};
string inputString = Console.ReadLine;
string input = inputString.Split(',');
const string QUIT = "quit";
Console.ReadLine();
Console.WriteLine("Enter equation");
equation[4] = Convert.ToDouble(Console.ReadLine());
while (equation.Length != 0)
{
if (equation[4] = QUIT)
Console.WriteLine("GoodBye");
while (oper != QUIT)
{
switch (equation)
{
case equation[0]:
total = num1 + num2;
Console.WriteLine("{0} {1} {2} = {3}", equation[0], total);
num1 = total;
break;
case "-":
Console.WriteLine("Enter number");
num2 = Convert.ToDouble(Console.ReadLine());
total = num1 - num2;
Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
num1 = total;
break;
case "*":
Console.WriteLine("Enter number");
num2 = Convert.ToDouble(Console.ReadLine());
total = num1 * num2;
Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
num1 = total;
break;
case "/":
Console.WriteLine("Enter number");
num2 = Convert.ToDouble(Console.ReadLine());
total = num1 / num2;
Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
num1 = total;
break;
case "=":
Console.WriteLine("Enter number");
num1 = Convert.ToDouble(Console.ReadLine());
break;
case "QUIT":
Console.WriteLine("Thank You for using my calculator!");
Console.ReadLine();
return;
default:
Console.WriteLine("Re-enter operation");
oper = Convert.ToString(Console.ReadLine());
break;
} Console.ReadLine();
}
}
}
}
}
This post has been edited by PsychoCoder: 12 May 2010 - 07:54 PM
Reason for edit:: Code tags added
#5
Re: using split within an array
Posted 12 May 2010 - 07:55 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project_2_2
{
class Program
{
static void Main()
{
double num1;
double num2;
double total;
string[] equation = { num1 + num2, num1 - num2, num1 * num2, num1 / num2 };
string inputString = Console.ReadLine;
string input = inputString.Split(',');
const string QUIT = "quit";
Console.ReadLine();
Console.WriteLine("Enter equation");
equation[4] = Convert.ToDouble(Console.ReadLine());
while (equation.Length != 0)
{
if (equation[4] = QUIT)
Console.WriteLine("GoodBye");
while (oper != QUIT)
{
switch (equation)
{
case equation[0]:
total = num1 + num2;
Console.WriteLine("{0} {1} {2} = {3}", equation[0], total);
num1 = total;
break;
case "-":
Console.WriteLine("Enter number");
num2 = Convert.ToDouble(Console.ReadLine());
total = num1 - num2;
Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
num1 = total;
break;
case "*":
Console.WriteLine("Enter number");
num2 = Convert.ToDouble(Console.ReadLine());
total = num1 * num2;
Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
num1 = total;
break;
case "/":
Console.WriteLine("Enter number");
num2 = Convert.ToDouble(Console.ReadLine());
total = num1 / num2;
Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
num1 = total;
break;
case "=":
Console.WriteLine("Enter number");
num1 = Convert.ToDouble(Console.ReadLine());
break;
case "QUIT":
Console.WriteLine("Thank You for using my calculator!");
Console.ReadLine();
return;
default:
Console.WriteLine("Re-enter operation");
oper = Convert.ToString(Console.ReadLine());
break;
}
}
}
Console.ReadLine();
}
}
}
#6
Re: using split within an array
Posted 12 May 2010 - 08:53 PM
I can do most any of the other problems in my book but this calculator is ripping me a new one. Please help.
#7
Re: using split within an array
Posted 12 May 2010 - 08:59 PM
#8
Re: using split within an array
Posted 12 May 2010 - 09:22 PM
Error 2 Cannot implicitly convert type 'string' to 'double'
Error 5 A value of an integral type expected
Error 6 A constant value is expected
I understand some what how to fix error 2 but I am having a hard time with it in this array.
#9
Re: using split within an array
Posted 12 May 2010 - 09:26 PM
double num1;
double num2;
double total;
string[] equation = { num1 + num2, num1 - num2, num1 * num2, num1 / num2 };
Explain that block of code. What's the point behind your array? What is num1 + num2? Is it a string?
#10
Re: using split within an array
Posted 12 May 2010 - 09:33 PM
I am trying to add to my code, an array, so that I can prompt for the equation and write it out in one line on the command prompt. I am just not grasping how.
#11
Re: using split within an array
Posted 12 May 2010 - 10:15 PM
|
|

New Topic/Question
Reply




MultiQuote





|