Making a program that converts Alphanumeric phone numbers to a regular phone number (ex.123-TRAVELS = 123-872-8357), but trying to figure some (probably simple to fix) errors to display the results under the CheckButton_Click variable. I'm giving myself a headache right now trying to figure this out. These are the current errors for the pasted code:
public static string PhoneToNumber(string str)
{
// Array version of value
var phoneNumberArr = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
phoneNumberArr[i] = PhoneCharToNumber(phoneNumberArr[i]);
}
return new string(phoneNumberArr);
}
public static char PhoneCharToNumber(char value)
{
switch (value)
{
case 'A':
case 'B':
case 'C':
return '2';
case 'D':
case 'E':
case 'F':
return '3';
case 'G':
case 'H':
case 'I':
return '4';
case 'J':
case 'K':
case 'L':
return '5';
case 'M':
case 'N':
case 'O':
return '6';
case 'P':
case 'Q':
case 'R':
return '7';
case 'S':
case 'T':
case 'U':
return '8';
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return '9';
default: return value;
}
}
private void CheckButton_Click(object sender, EventArgs e)
{
string str = inputTextBox.Text;
char value = PhoneCharToNumber(value);
resultsLabel.Text = value;
}
private void ExitButton_Click(object sender, EventArgs e)
{
// Closes the form
this.Close();
}
}
The error is here: char value = PhoneCharToNumber(value);
where value argument for PhoneCharToNumber hasn't been declared nor assigned.
I think you wanted to use string value = PhoneToNumber(str); instead.
Infact your user enters number inside inputTextBox; you have to convert that text to a valid number and then place the result to resultsLabel.
private void CheckButton_Click(object sender, EventArgs e)
{
string str = inputTextBox.Text;
string value = PhoneToNumber(str);
resultsLabel.Text = value;
}
In the CheckButton_Click method, you pass value as parameter. Value is undefined at that moment, you should pass str (which is a bad name) like this. Beside that PhoneCharToNumber returns a string not a char:
private void CheckButton_Click(object sender, EventArgs e)
{
string textToProcess = inputTextBox.Text;
string processedText = PhoneToNumber(textToCheck );
resultsLabel.Text = processedText ;
}
Related
I am building a form that takes an input number from a text box and then will take the number that was input, and display the roman numeral equivalent in another text box.
My Form:
private void convertButton_Click(object sender, EventArgs e)
{
int numberInput;
switch (numberInput)
This is where I keep getting an error code. The "switch (numberInput)" is seen as and unassigned local variable. How do I assign it so that it will be able to access all of the case integers?
{
case 1:
outputTextBox.Text = "I";
break;
case 2:
outputTextBox.Text = "II";
break;
case 3:
outputTextBox.Text = "III";
break;
case 4:
outputTextBox.Text = "IV";
break;
case 5:
outputTextBox.Text = "V";
break;
case 6:
outputTextBox.Text = "VI";
break;
case 7:
outputTextBox.Text = "VII";
break;
case 8:
outputTextBox.Text = "VIII";
break;
case 9:
outputTextBox.Text = "IX";
break;
case 10:
outputTextBox.Text = "X";
break;
default:
MessageBox.Show("Please enter and number between 1 and 10. Thank you!");
break;
}
Cause your variable is not assigned yet int numberInput; and so the error. You said input is coming from a TextBox, in that case do like below assuming textbox1 is your TextBox control instance name
int numberInput = Convert.ToInt32(this.textbox1.Text.Trim());
Convert.ToInt32 may throw an exception if the parsing is unsuccessful. Another method is to Int.Parse:
int numberInput = int.Parse(textbox1.Text.Trim());
or better yet
int numberInput;
if(int.TryParse(textbox1.Text.Trim(), out numberInput))
{
switch (numberInput)
...
}
I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
Console.;
break;
case 1:
Console.WriteLine.TextBox("Yes");
break;
case 2:
Console.WriteLine.TextBox("No");
break;
case 3:
Console.WriteLine.TextBox("Maybe");
break;
case 4:
Console.WriteLine.TextBox("You could say that");
break;
case 5:
Console.WriteLine.TextBox("Most certain");
break;
case 6:
Console.WriteLine.TextBox("Dont even try");
break;
case 7:
Console.WriteLine.TextBox("Full steam ahead");
break;
}
}
Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...
Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
theTextBox.Text = "";
break;
case 1:
theTextBox.Text = "Yes";
break;
case 2:
theTextBox.Text = "No";
break;
case 3:
theTextBox.Text = "Maybe";
break;
case 4:
theTextBox.Text = "You could say that";
break;
case 5:
theTextBox.Text = "Most certain";
break;
case 6:
theTextBox.Text = "Dont even try";
break;
case 7:
theTextBox.Text = "Full steam ahead";
break;
}
}
Consider following case statement
Case 'A':
break;
Case 'B':
Case 'C':
// some logic
int i = 0;
// here I need i =5 (if case id 'B') and i=10 (if case is 'C')
// Rest of the logic is same
break;
I know I can achieve this by writing seperate case for 'B' and 'C' and writing rest of the logic in a seperate function and call that function in 'B' and 'C' case.
But is there any way, I can check the case in Case statement only ... as follows
Case 'B':
Case 'C':
// Can I check here
// if (case == 'B')
// i = 5;
// if (case == 'C')
// i = 10;
// Rest of the logic
I tried the following which worked without any problems:
switch (a)
{
case 'A':
Console.WriteLine("Es ist ein 'A'.");
break;
case 'B':
case 'C':
if (a == 'B')
Console.WriteLine("Es ist ein 'B'.");
if (a == 'C')
Console.WriteLine("Es ist ein 'C'.");
break;
}
But if you're just checking if it's 'B' or 'C', I would suggest writing two separate cases.
Put your //Rest of Logic inside a new method, and do separate test cases for a neater code:
private void DoSomething(int i)
{
//Rest of Logic
}
public void SwitchMethod(char input)
{
int i = 0;
Switch (input)
{
case 'A':
break;
case 'B':
i = 5;
DoSomething(i);
break;
case 'C':
i = 10;
DoSomething(i);
break;
}
}
I've made a program where I can decrypt some encrypted text, it does this by reading a text file and shifting the text one letter at a time, and it does this 26 times, but I can't get it to save and write the output to a text file, this is my code below, I would appreciate any help if possible, thank you
using System;
using System.IO;
namespace Assignment1
{
class MainClass
{
public static void Main(string[] args)
{
//Writing to the screen
Console.WriteLine("Welcome to the Ceaser Cipher Shift Program");
Console.WriteLine("Would You Like to Decrypt a File (y for yes/n for no)");
string userValue = Console.ReadLine();
//User's value is set to userValue
// if the user types "y", activate the decryption method
if (userValue.ToLower() == "y")
{
decryption();
}
// if the user types in "n", write "Goodbye" and close the program
if (userValue.ToLower() == "n")
{
Console.WriteLine("Goodbye");
Console.ReadLine();
Environment.Exit(0);
//Environment.exit closes the program
}
}
//Decryption method
public static void decryption()
{
//ShiftLine is equal to new char
char[] ShiftLine = new char[0];
//If the shift is smaller or equal to 25, continue to shift one at a time
for (int shift = 1; shift <= 26; shift++)
{
//This reads the encryptefd text file into the program
string textFile = #"C:\Users\Anthony\Desktop\caesarShiftEncoded.txt";
//The string "test" reads all the lines of the textFile
string[] text = File.ReadAllLines(textFile);
foreach (string line in text)
{
//Sets currentLetter to 0
int CurrentLetter = 0;
int[] ShiftNumbers = new int[line.Length];
ShiftLine = new char[line.Length];
foreach (char letter in line)
{
ShiftNumbers[CurrentLetter] = ConvertLetterToNumber(letter);
ShiftNumbers[CurrentLetter] = ShiftCipher(ShiftNumbers[CurrentLetter], shift);
ShiftLine[CurrentLetter] = ConvertNumberToLetter(ShiftNumbers[CurrentLetter]);
CurrentLetter++;
}
Console.WriteLine(string.Join("", ShiftLine));
}
//Console.WriteLine (textFile.Length);
Console.WriteLine("This is Shift No: {0}", shift);
}
Console.WriteLine("Which Shift Would You Like To Write to a Text File: ");
string userNumber = Console.ReadLine();
if (userNumber.ToLower() == "1 =< 26")
{
using (StreamWriter text = new StreamWriter("TextWrittenFile.txt"))
{
foreach (char line in ShiftLine)
{
text.WriteLine(ShiftLine);
File.WriteAllText("C:\Users\Anthony\Desktop\DecryptedText.txt", ShiftLine);
}
}
}
}
public static int ShiftCipher(int Number, int Shift)
{
if (Number == 27)
{
return 27;
}
else if (Number == 28)
{
return 28;
}
else if (Number > Shift)
{
return (Number - Shift);
}
else if (Number <= Shift)
{
return (26 + (Number - Shift));
}
else
{
return 0;
}
}
public static int ConvertLetterToNumber(char Letter)
{
switch (Char.ToLower(Letter))
{
case 'a':
return 1;
case 'b':
return 2;
case 'c':
return 3;
case 'd':
return 4;
case 'e':
return 5;
case 'f':
return 6;
case 'g':
return 7;
case 'h':
return 8;
case 'i':
return 9;
case 'j':
return 10;
case 'k':
return 11;
case 'l':
return 12;
case 'm':
return 13;
case 'n':
return 14;
case 'o':
return 15;
case 'p':
return 16;
case 'q':
return 17;
case 'r':
return 18;
case 's':
return 19;
case 't':
return 20;
case 'u':
return 21;
case 'v':
return 22;
case 'w':
return 23;
case 'x':
return 24;
case 'y':
return 25;
case 'z':
return 26;
case ' ':
return 27;
default:
return 0;
}
}
public static char ConvertNumberToLetter(int Number)
{
switch (Number)
{
case 1:
return 'a';
case 2:
return 'b';
case 3:
return 'c';
case 4:
return 'd';
case 5:
return 'e';
case 6:
return 'f';
case 7:
return 'g';
case 8:
return 'h';
case 9:
return 'i';
case 10:
return 'j';
case 11:
return 'k';
case 12:
return 'l';
case 13:
return 'm';
case 14:
return 'n';
case 15:
return 'o';
case 16:
return 'p';
case 17:
return 'q';
case 18:
return 'r';
case 19:
return 's';
case 20:
return 't';
case 21:
return 'u';
case 22:
return 'v';
case 23:
return 'w';
case 24:
return 'x';
case 25:
return 'y';
case 26:
return 'z';
case 27:
return ' ';
default:
return '0';
}
}
}
}
As your ShiftLine seems to be an array of chars it could be as simple as that:
File.WriteAllText("C:\Users\Anthony\Desktop\DecryptedText.txt", new string(ShiftLine));
(instead of your loop [btw: Your loop overwrites the file on any turn...])
Actually there are a few things you could improve.
If the user shall only be able to press y or n, you could use the Console.ReadKey() method instead of letting the user input a complete line of text. By using the overloaded method and specifying true as argument, you won't even see the pressed key on written into the console window (Console.ReadKey(true)).
So you could exchange the current if statements with switch (Console.ReadKey(true).Key) and build cases like case ConsoleKey.Y:
So the Main method would look something like that:
public static void Main(string[] args)
{
//Writing to the screen
Console.WriteLine("Welcome to the Ceaser Cipher Shift Program");
Console.WriteLine("Would You Like to Decrypt a File (y for yes/n for no)");
switch (Console.ReadKey(true).Key)
{
// if the user types "y", activate the decryption method
case ConsoleKey.Y:
decryption();
break;
// if the user types in "n", write "Goodbye" and close the program
case ConsoleKey.N:
default:
Console.WriteLine("Goodbye");
Environment.Exit(0);
break;
}
}
I guess the next thing you want to do is using Caesar cipher for encrypting/decrypting data.
Therefore you "just" need to shift every character and this can be done much easier than writing switch-case statements like you did. The idea behind this type of encryption is, that you take one character and replace it with another one which is offset characters away. Decrypting goes the other way.
So it would be much easier building one method that has the ability to do exactly that for a specified text.
There are two special cases you need to consider: What happens if the value you are looking at is on the lower boundary and you substract the offset (meaning, the "new" value would be lower than zero). The second one represents the same situation on the upper boundary.
These sitations can be resolved using either if statements or possibly easier by just looking at the rest of the division with the maximum value range. (text[i] + offset) % NumberOfPossibleCharacters should do the trick.
Knowing this you can easily build a method like the following one (where I have assumed, that there is no character with a value greater than 127):
static string Encrypt(string data, int offset)
{
string result = string.Empty;
for (int i = 0; i < data.Length; i++)
{
// Add the offset to the current character and just take the
// rest of the division by 127. Afterwards cast it back to a character
// (because it will be a number due to + and %)
result += (char)((data[i] + offset) % 127);
}
return result;
}
Be advised, that string operations like += will cause the creation of a new string each time so it is not a good example from an (memory)performance point of view. There is the better way of using the StringBuilder class.
I hope this hint's you in a better way of solving your homework ;-)
This question already has answers here:
Unreachable code detected in case statement
(14 answers)
Closed 7 years ago.
I'm trying to convert string to int. I think I have it right, but what else do I need to do? Under "Break" I get a green line saying "unreachable code detected". Also, what do I tell it to return? I put a random number after return, because I have blanked on what I should ask it to return.
namespace BattleShip.UI
{
class TranslateNumberToLetter
{
public int NumberToLetter(string Letter)
{
switch (Letter)
{
case "A":return 1;
break;
case "B": return 2;
break;
case "C": return 3;
break;
case "D": return 4;
break;
case "E": return 5;
break;
case "F": return 6;
break;
case "G": return 7;
break;
case "H": return 8;
break;
case "I": return 9;
break;
case "J": return 10;
break;
default: return -100;
}
}
}
}
public int NumberToLetter(string Letter)
{
if ("ABCEDFGHIJ".Contains(Letter))
return "ABCEDFGHIJ".IndexOf(Letter) + 1;
return -100;
}