473,503 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

If statement for Console.ReadLine()

1 New Member
I've made this console program as a small start for an up comming text based AI, in this code, I'm trying to find the sentence that the user inputs, and then display a message from that input.

My problem here is that, if i type in "Hello" as the first word and press enter, it works as i should, but if i type in "Hi" as the first word, nothing happens... If i then try to type in "Hi" as the second input, it comes with the matching output.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Alicia
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         { 
  13.             string firstName = "Alicia";
  14.             string[] myText = new string[6];
  15.             myText[0] = "Hello";
  16.             myText[1] = "Hi";
  17.             myText[2] = "Hello, who are you?";
  18.             myText[3] = "Hi, who are you?";
  19.             myText[4] = "Hello, what is your name?";
  20.             myText[5] = "Hi, what is your name?";
  21.  
  22.             Console.WriteLine("Hello User");
  23.  
  24.             if(Console.ReadLine() == myText[0])
  25.             {
  26.                 Console.WriteLine("What is your name User?");
  27.             }
  28.  
  29.             else if(Console.ReadLine() == myText[1])
  30.             {
  31.                 Console.WriteLine("What is your name User?");
  32.             }
  33.  
  34.             else if(Console.ReadLine() == myText[2])
  35.             {
  36.                 Console.WriteLine("I'm {0}, who are you?", firstName);
  37.             }
  38.  
  39.             else if(Console.ReadLine() == myText[3])
  40.             {
  41.                 Console.WriteLine("I'm {0}, who are you?", firstName);
  42.             }
  43.  
  44.             else if(Console.ReadLine() == myText[4])
  45.             {
  46.                 Console.WriteLine("My name is {0}, what is yours?", firstName);
  47.             }
  48.  
  49.             else if (Console.ReadLine() == myText[5])
  50.             {
  51.                 Console.WriteLine("My name is {0}, what is yours?", firstName);
  52.             }
  53.         }
  54.     }
  55. }
  56.  
Mar 31 '17 #1
1 3812
Frinavale
9,735 Recognized Expert Moderator Expert
You have an array of strings.

After you populate your array, and displaying a message saying "Hello User" you are reading the line of text that the user provides within your if statement. This compares against the first element in your array (which is "Hello" and has to match this exactly) then your program displays the next prompt.

Consider reading the user's input into a variable to match against expected replies...looping until the user inputs something valid (and using the String.Compare method so that you don't have to match case exactly).

After the user inputs something valid, you can check which case the user entered to output your reply.


For example:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Alicia
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         { 
  13.             string firstName = "Alicia";
  14.             string[] myText = new string[6];
  15.             myText[0] = "Hello";
  16.             myText[1] = "Hi";
  17.             myText[2] = "Hello, who are you?";
  18.             myText[3] = "Hi, who are you?";
  19.             myText[4] = "Hello, what is your name?";
  20.             myText[5] = "Hi, what is your name?";
  21.  
  22.  
  23.             string userInput;
  24.             bool userInputIsValid = false;
  25.             Console.WriteLine("Hello User");
  26.  
  27.             while(!userInputIsValid){
  28.                 userInput = Console.ReadLine();
  29.                 for(int textIndex = 0; textIndex < myText.Length; textIndex++)
  30.                 {
  31.                     if(userInput.ToLower() == myText[textIndex].ToLower())
  32.                     {
  33.                         userInputIsValid = true;
  34.                     }
  35.                 }
  36.  
  37.                 if(!userInputIsValid){
  38.                     Console.WriteLine("I'm sorry, I don't understand. Please rephrase.");
  39.                 }
  40.             }
  41.  
  42.  
  43.             if( userInput.ToLower() == myText[0].ToLower() || userInput.ToLower() == myText[1].ToLower()
  44.             {
  45.                 Console.WriteLine("What is your name User?");
  46.             }
  47.             else if(userInput.ToLower() == myText[2].ToLower() || userInput.ToLower() == myText[3].ToLower()
  48.             {
  49.                 Console.WriteLine("I'm {0}, who are you?", firstName);
  50.             } 
  51.             else if(userInput.ToLower() == myText[4].ToLower() || userInput.ToLower() == myText[5].ToLower())
  52.             {
  53.                 Console.WriteLine("My name is {0}, what is yours?", firstName);
  54.             }
  55.         }
  56.     }
  57. }
Apr 10 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6090
by: talljames | last post by:
Hi all, Does anyone know how to end a Console.ReadLine() programmically rather than typing the enter key in C#? I have tried just setting the class object that contains this line to null, but...
0
1387
by: Vampier | last post by:
Hello, I don't know if my professor pulled a late Aprils fools joke on us or not. Here is my 'problem': I am supposed to enter 3 numbers without entering the return key, I can only enter the...
1
5170
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
8
4548
by: nadeem_far | last post by:
Hello All, These days I am working on a console application that will register for certain events and do some processing based on the event notification. Now, all the objects are being created...
1
4916
by: Joachim | last post by:
Is there a way to set a timeout for the Console.ReadLine method?
2
14318
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to...
0
1001
by: needhelpnow | last post by:
Hi, I have a webservice client (win app) which talks to a server (console app) via a webservice server, things work fine until I decided to add a Console.ReadLine() to get some input interactively...
1
1689
by: Allen Maki | last post by:
Hi everybody, I need your help. I am using Visual C++ .NET How can I make Console::ReadLine accepts int? In other words. I know I can do this:
2
4872
by: elvan | last post by:
using System; namespace namespace1 { class class1 { static void Main(String args) { Console.WriteLine("What is your initial?"); int name1 =...
2
5968
by: Themantimes8 | last post by:
I am making a game that requires input to be taken until the user inputs "exit" to the console. That part is taken care of with a simple do-while loop where i ask for user input: do { ...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.