Hi i'm currently using Thread.Sleep(200); in between text for it to look like old pokemon game with the text not appearing all at once but going a bit laggy word for word like this
Console.Write("help"); Thread.Sleep(200); Console.Write("me");
And i'm wondering if you can name/shorten the Thread.Sleep(200) to something like Ts200. (I mean to text). So it's not much clutter.
So it would be something like this with a string.
string Ts200
Ts200 = Thread.Sleep(200)
Console.Write("help"); Ts200; Console.Write("me")
But it's not working.
I get this error:
cannot implicitly convert type void to string
a simple way to do this would be to split the strings into arrays on spaces, then loop over them.
using System;
using System.Threading;
public static void slowType(string fullSentence, int millis_pause)
{
string[] words = fullSentence.Split(' ');
foreach(string s in words)
{
Console.Out.Write(s);
Thread.Sleep(millis_pause);
//we must reintroduce the space, since we were splitting on it.
Console.Out.Write(' ');
}
//optionally ; start a new line at the end of the sentence
Console.Out.Write("\r\n");
}
usage example :
public static void Main()
{
string sentence = "i am a terribly slow typist";
slowType(sentence, 200);
}
Try lambda if you don't mind () after Ts200:
Action Ts200 = () => Thread.Sleep(200);
...
Ts200();
However, even if it's possile Ts200() is far less readable than Thread.Sleep(200), that's why don't do it.
You call the Thread.Sleep(200); on its own, it doesnt contain any value. So you cant return any value neither can you allocate it any value, except as the method parameter Thread.Sleep("Here belongs the method parameter, this method accepts an 'int' value"). The int method parameter sets for how many milli seconds you want to pause the mainthread.
Try something like this:
The main method is the entry point in your application once it gets executed. So when you start your application, you call the main method and execute it.
static void Main(string[] args)
{
string sentence = "Here you enter your sentence...";
string[] words = sentence.Split(' ');
OutputDelayedText(words);
}
The string sentence is the string variable that contains the sentence you want to ouput on the console. So we split the sentence into single words with the method .Split(' '). This splits the spring on each space ' ' and puts the single words into a string[] array. Note that you have to use in the .Split('') method these '' instead of what you are probably used to "". Thats because the method accepts a char value and those '' indicate that you just put a single char in as the method parameter.
So in the end we call the OutputDelayedText() method with our word array as method parameter.
private static void OutputDelayedText(string[] myText)
{
foreach (var word in myText)
{
Console.Write(word + " ");
Thread.Sleep(200);
}
}
Now we use a foreach loop to iterate over the array and write every single element of our array (one single word of our sentence) in one line on the console. If you are not familiar with foreach you could use either a for or while loop as well.
In generell i advice you to use Task.Delay(). Or in WinForms the Backgroundworker, those will allow you to work on different threads and if you pause those threads you dont pause your main thread. You will come across this issue later, this is used to pause any process or to execute a long task in the background to not freeze your GUI when the task is still running.
However just start with Thread.Sleep(), but keep in mind: In the art of programming are so many ways how to achieve your goal and you might stumble quite often over a better solution.
Related
I am having problem in this program that i have. i want to be able to print the random words from the void Spoken() method and also to be able to return the number of words that are counted using the Main() method.
static void Main ()
{
Spoken ();
string sentence = Spoken ();
string[] words = sentence.Split (' ');
Console.WriteLine("Words counted: " + words.Length);
}
static void Spoken ()
{
var wordss = new string[]{
"\nfine day\n",
"\nnight time\n",
"\nexclusive place to unpack\n",
"\ndoing better at clicking\n",
"\nkilling time?\n",
};
var ran = new Random ();
var pc = ran.Next (words.Length);
Console.WriteLine (words[pc]);
}
sample output:
exclusive place to unpack
Words counted: 4
what should i do? and is there a way? any help would be much appreciated
Change the method declaration to return string and then return words[pc] from the method.
Or if you want to return only the count if words then keep the Console.WriteLine in the Spoken method and just return the array length fr the method.
If only printing to the console is the only motive then you can keep the method as returning void and print inside the method only.
And if the intention is to just print the words and count then you can avoid Split. You can just run a for loop and check for the IsWhiteSpace and keep on incrementing counter value.
I'm just doing a little project in C# (I'm a beginner), my code is basically asking you "how many words are in this sentence?" and then asks you for every word, once it gets all of them it prints it out with "ba" attached to every word.
I know I'm a real beginner and my code's probably a joke but could you please help me out with this one?
Console.WriteLine("How many words are in this sentence?");
int WordAmount = Convert.ToInt32(Console.ReadLine());
int i = 1;
while (i <= WordAmount)
{
Console.WriteLine("Enter a word");
string[] word = new string[] { Console.ReadLine() };
i++;
}
Console.WriteLine(word + "ba");
You're close, you've just got one issue.
string[] word = new string[] { Console.ReadLine() };
You are creating a new array list inside the scope of a while loop. Not only will this disappear every loop, meaning you never save the old words, but you also won't be able to use it outside of the loop, making it useless.
Create a string[] words = new string[WordAmount];. Then iterate through it to add your Console.ReadLine() to it, and finally, iterate through it once more and Console.WriteLine(words[i] + "ba");
string[] wordList = new string[WordAmount];
while (i <= WordAmount)
{
Console.WriteLine("Enter a word");
wordList[i-1] = Console.ReadLine() ;
i++;
}
foreach (var item in wordList)
Console.WriteLine(item + "ba");
Working Fiddle: https://dotnetfiddle.net/7UJKwN
your code has multiple issues. First you need to define your array outside of your while loop, and then fill it one by one.
In order to read/write array of strings (string[]), you need to loop through (iterate) it.
My code actually iterates your wordList. In the first While loop I am iterating to fill the wordList array. then printing it in the second loop
First of all, consider storing your words in some kind of collection, for example a list.
List<string> words = new List<string>();
while (i <= WordAmount)
{
Console.WriteLine("Enter a word");
string word = Console.ReadLine();
words.Add(word);
i++;
}
I don't think your code compiles - the reason is you are trying to use the word variable outside of the scope that it is defined in. In my solution I have declared and initialized a list of strings (so list of the words in this case) outside of the scope where user has to input words, it is possible to access it in the inner scope (the area between curly brackets where user enters the words).
To print all the words, you have to iterate over the list and add a "ba" part. Something like this:
foreach(var word in words)
{
Console.WriteLine(word + "ba");
}
Or more concisely:
words.ForEach(o => Console.WriteLine(o + "ba"));
If you want to print the sentence without using line breaks, you can use LINQ:
var wordsWithBa = words.Select(o => o + "ba ").Aggregate((a, b) => a + b);
Console.WriteLine(wordsWithBa);
Although I would recommend learning LINQ after you are a bit more familiarized with C# :)
You can look here and here to familiarize yourself with the concept of collections and scopes of variables.
You could also use a StringBuilder class to do this task (my LINQ method is not very efficient if it comes to memory, but i believe it is enough for your purpose).
So I have a very simple bit of code
public static string[] config = File.ReadAllLines(Environment.CurrentDirectory.ToString() + "\\documents\\config.json");
public static void Start()
{
Console.WriteLine(config[4]);
Console.ReadKey();
}
This properly displays the 5th item in the array, which is "0x00=jU0UrZBkqPXfp8MsMoILSRylevQGaUmJRnpFbfUvcGs=7lvpCgtyWl0 : crypt_wallet". I only want the first part of the string, so "0x00=jU0UrZBkqPXfp8MsMoILSRylevQGaUmJRnpFbfUvcGs=7lvpCgtyWl0".
When I use Console.WriteLine(config[4].split(null); or anything else in the split arguments, I just get back System.String[].
How would I fix that so it properly displays the answer?
Should rather be like below since you will have to choose the element to print since Split() returns a string[]
Console.WriteLine(config[4].Split(':')[0]);
Not sure what you mean by the "the first part of the string". But you can achieve this by using .Substring().
If you know that the beginning will always be a fixed length, you can do:
config[4].Substring(0, 4);
If you know the value will be followed by a certain character (like the "=" for an example:
config[4].Substring(0, config[4].IndexOf("="));
Using this, I'm trying to print a list of the contents of players.text
I know that the foreach loop will print it just fine if the Console.Writeline
is used. But I'm attempting to manipulate the data in particular ways later, and need it split using the indicated delimiter.
However, with the given code all I get is a repeated System.String[] printout.
I've done a search or two and most of what I've found has to do with one part or the other, I haven't found any information on how to use them together.
string[] playerFile = File.ReadAllLines("players.txt");
foreach (string s in playerFile)
{
//Console.WriteLine(s);
string[] playerStuff = s.Split(';');
Console.WriteLine(playerStuff);
}
Console.ReadKey();
I realize it's a simplistic question. But often, for me at least, it's missing the obvious that drives me the most crazy.
Thanks in advance.
Player;Team;POS;HR;RBI;AVG
Abreu, J;CWS;1B;30;101;0.29
Altuve, J;HOU;2B;15;66;0.313
Andrus, E;TEX;SS;7;62;0.258
Arenado, N;COL;3B;42;130;0.287
Aybar, E;LAA;SS;3;44;0.27
The above is the first few lines of the input.
Basically, I want it to look just like that, minus the semicolons. Formatting will come later.
Attempting to add a second foreach, such as was suggested below, the code looked like this:
foreach (string s in playerFile)
{
//Console.WriteLine(s);
string[] playerStuff = s.Split(';');
foreach (string player in playerStuff)
{
Console.WriteLine(player);
}
}
This resulted in EACH piece of information getting it's own line. I follow the logic of why it did that, but I'm not sure what to do about it.
For a method call to compile the compiler has to figure out what you want to do and here, in particular, there are many overloads to the Console.WriteLine method. An "overload" basically means that there are several definitions of Console.WriteLine, all taking different types of parameters.
If the compiler can find an overload that takes exactly what you're trying to pass it, in this case string[], then good, that's the overload that will be called.
If not then it will take "the next best thing", if at all possible.
Let's list all the overloads to Console.WriteLine:
WriteLine()
WriteLine(Boolean)
WriteLine(Char)
WriteLine(Char[])
WriteLine(Char[], Int32, Int32)
WriteLine(Decimal)
WriteLine(Double)
WriteLine(Int32)
WriteLine(Int64)
WriteLine(Object)
WriteLine(Single)
WriteLine(String)
WriteLine(String, Object)
WriteLine(String, Object, Object)
WriteLine(String, Object, Object, Object)
WriteLine(String, Object, Object, Object, Object)
WriteLine(String, Object[])
WriteLine(UInt32)
WriteLine(UInt64)
Now, now of these will be able to accept a string[] except one:
WriteLine(Object)
This particular overload will simply call .ToString() on whatever it is passed.
Since a string array does not have a ToString implementation that looks at the contents of the array, or anything like that, the basic System.Object.ToString() method that every type inherits is used, and this simply returns the full name of the type as a string, which is basically System.String[].
So that's why the code compiles, and also why it doesn't do what you expect it to do.
The question is, what do you expect it to do?
If you wanted it to output all the strings as a comma-separated set of values on each line you can do this:
Console.WriteLine(string.Join(", ", playerStuff));
If you simply wanted to concatenate all the strings and put nothing between them, replace the ", " in the above statement with "" or string.Empty:
Console.WriteLine(string.Join(string.Empty, playerStuff));
The reason you are getting the output that you are is because it is trying to write the Array object to the console; and it doesn't know how to handle it, so it is just printing out the Type of the object that it is trying to write out.
You need to do another foreach or something similar on playerStuff.
string[] playerFile = File.ReadAllLines("players.txt");
foreach (string s in playerFile)
{
//Console.WriteLine(s);
string[] playerStuff = s.Split(';');
//Inner foreach that writes out each entry of the array
foreach(var item in playerStuff)
{
Console.Write(item + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
OR even simpler
string[] playerFile = File.ReadAllLines("players.txt");
foreach (string s in playerFile)
{
Console.WriteLine(s.Replace(';', ' '));
}
Console.ReadKey();
OR a one liner
string[] playerFile = File.ReadAllLines("players.txt");
playerFile.ForEach(x => Console.WriteLine(x.Replace(';', ' ')));
Console.ReadKey();
Sample output image for all 3 of the above solutions:
Console.WriteLine docs for reference
String.Replace docs for reference
I have a function in a class called Function, like below:
public int SearchedRecords(String [] recs)
{
int counter = 0;
String pat = "-----";
String[] records = recs;
foreach (String line in records)
{
if (line.Contains(pat) == true)
{
counter++;
}
}
return counter;
}
And I am calling this method from my main class this way:
String [] file = File.ReadAllLines("C:/Users.../results.txt");
int counter = Function.SearchedRecords( []file);
But I get an error saying:
;expected
What is wrong?
Another question: The function above is counting from a file all the lines with the pattern ----- in them (even if with more dashes, or if the line has some chars before or after the dashes). Am I right?
It's something like the patterns in Java so maybe there is an other way.
Can you enlighten me?
Remove the [] from your parameter.
e.g.
int counter = Function.SearchedRecords(file);
And yes, your assumption about the behavior of the Contains method is correct - you'll match any line containing five consecutive dashes, regardless of what characters are before or after them.
If you want to parse for exactly five dashes, with nothing before or after them I suggest looking into the RegEx class (regular expressions).
Change
int counter = Function.SearchedRecords( []file);
to
int counter = Function.SearchedRecords(file);
and yes, this will work, for that string.
However Contains is case sensitive, if you were matching on a name, or another string with alphabetic characters, the case would have to be identical to match e.g. line.Contains("Binary Worrier") will not match a string "Hello binary worrier".
Also, reading the entire file into memory is fine if you know that the file will always be small, this method gets less efficient the larger the file.
Better to always use something like System.IO.StreamReader or System.IO.File.ReadLines (available in .Net 4 and later), these allow you to consume the file one line at a time. e.g.
using (var reader = new System.IO.StreamReader("MyFile.txt"))
{
while(!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line.Contains(pattern))
counter++;
}
}
Change it to
int counter = Function.SearchedRecords(file);
Remove '[]' from a method call. Yes, your function seems to count what you want.
First of all you need to create an instance of function class and then run the function. Hope following code helps
Function fb = new Function();
int counter = fb.SearchedRecords(file);
Right now, you are using SearchRecords as an static function of a static class which doesn't require instantiation.
You can do this in a shorter way using LINQ:
int counter = file.Count(line => line.Contains("-----"));