I tried to run a sample program from dotnetpearls.com and at first the program didn't work at all.
Apparently I had to run VS Express 2012 as Administrator, before I could start an Application object. After that, the next time it errors out, is when I try to print out the text from the document. Error happens at string text = doc.Words[i].Text;
using System;
using Microsoft.Office.Interop.Word;
namespace WordTestProgram
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
Document doc = app.Documents.Open("C:\\word.doc");
int count = doc.Words.Count;
for (int i = 0; i <= count; i++)
{
string text = doc.Words[i].Text;
Console.WriteLine("Word {0} = {1}",i,text);
}
app.Quit();
}
}
}
I know for a fact that the document I am trying to extract data from, does have 3 words and 3 spaces in it. So it's not empty.
I found the answer myself
Instead of: int i = 0; i <= count; i++
I should do: int i = 1; i <= count; i++
Apparently member 0 in the array is null and the program can't handle that.
Related
I am having an issue with picking randomly from a list. When I run the program, it keeps writing the same string over and over again when I want it to come up with a different random string each time. Here is an example:
using System;
using System.Collections.Generic;
namespace List
{
class Program
{
static void Main(string[] args)
{
var letterList = new List<string>
{
"A","B","C","D"
};
Random r = new Random();
int letterListIndex = r.Next(letterList.Count);
for (int i = 0; i < 10; i++) {
Console.WriteLine(letterList[letterListIndex]);
}
}
}
}
For Example: When I run this code it would write "B" 10 times. I want it to come up with 10 different letters every time. I know that you could just write:
int letterListIndex1 = r.Next(letterList.Count);
int letterListIndex2 = r.Next(letterList.Count);
int letterListIndex3 = r.Next(letterList.Count);
Console.WriteLine(letterList[letterListIndex1]);
Console.WriteLine(letterList[letterListIndex2]);
Console.WriteLine(letterList[letterListIndex3]);
But I wanted to know if there was an easier way to do so.
Thanks.
You should put your letterListIndex variable inside for loop
for (int i = 0; i < 10; i++) {
int letterListIndex = r.Next(letterList.Count);
Console.WriteLine(letterList[letterListIndex]);
}
Otherwise you get the same index every time.
You need to put
int letterListIndex = r.Next(letterList.Count);
inside the for loop.
you can use below code
for (int i = 0; i < 10; i++) {
Console.WriteLine(letterList[r.Next(letterList.Count)]);
}
I've been trying to make some kind of message parser, that gets only my sent messages. For example if i had message like that:
Viktor Bale (11 aug. 2016 13:20:56):
Hi! How are you?
Not Viktor Bale (11 aug. 2016 13:20:56):
Hi! Good! And you?
Viktor Bale (11 aug. 2016 13:20:56):
Me too! And this message has
Two lines!
Not Viktor Bale (11 aug. 2016 13:20:56):
And this doesn't matter!
I need to get only messages written by Viktor Bale
here is code, that i tryed:
for (int i = 0; i < wordsList.Count; i++)
{
if (wordsList[i].StartsWith(defaultName))
{
while (!wordsList[i].StartsWith(dialName))
{
messages.Add(wordsList[i]);
}
}
}
wordsList is list of my messages, recieved from txt file and read by ReadAllLines
So messages above is just list.
defaultName is my name, and dialName is name of my interlocutor.
But when i launch it, my app simply freezes. How should i do that?
You are forgetting to increment i:
for (int i = 0; i < wordsList.Count; i++)
{
if (wordsList[i].StartsWith(defaultName))
{
while (i < worldList.Count && !wordsList[i].StartsWith(dialName))
{
messages.Add(wordsList[i++]);
}
}
}
Edit: Added a safety bounds check.
The while loop will never come to an end.
Perhaps you meant something like this? I've tidied up your code and made it simpler.
foreach (var words in wordsList)
{
if (words.StartsWith(defaultName) && !words.StartsWith(dialName))
{
messages.Add(wordsList[i]);
}
}
you should be able to select your messages using linq assuming that each line begins with the name of the sender and the message does not include a line break.
e.g.
var myMessages = wordsList.Where(x => x.StartsWith(defaultName))
The application is crashing on your while loop which simply evaluates a condition infinity but never does anything to change it.
To avoid endless while loop, use this code instead :
for (int i = 0; i < wordsList.Count; i++)
{
if (wordsList[i].StartsWith(defaultName))
{
if (!wordsList[i].StartsWith(dialName))
{
messages.Add(wordsList[i]);
}
}
}
OR
you can use something simpler to achieve desired behaviour:
foreach (var word in wordsList)
{
if (word.StartsWith(defaultName))
{
messages.Add(word);
}
}
hope it helps
Here is an alternative to do that so:
public static string ExtractSenderName(string line) {
var i = line.IndexOf('(');
if (i == -1)
return string.Empty;
return line.Substring(0, i).Trim();
}
public static void Main (string[] args) {
var messages = new List<string>();
for (int i = 0; i < wordsList.Length; i++)
{
if (ExtractSenderName(wordsList[i]) == defaultName) {
messages.Add(wordsList[++i]);
}
}
foreach (var x in messages) {
Console.WriteLine(x);
}
}
Here is the demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sisesta suvaline tekst-->");
string tekst1 = Console.ReadLine();
// string tekst2 = ("ja");
char jtaht = ('j');
char ataht = ('a');
int jsidOntekstis = 0;
int asidOnTekstis = 0;
int tekstipikkus = tekst1.Length;
int jasidonTekstis = jsidOntekstis + asidOnTekstis;
int jasidEiOleTekstis=1;
for (int i = 0; i < tekstipikkus; i++)
{
if (tekst1[i] == jtaht)
{
jsidOntekstis++;
}
if (tekst1[i] == ataht)
{
asidOnTekstis++;
}
}
// for (int k = 0; i < tekstipikkus; i++)
{
{
if (jasidonTekstis > jasidEiOleTekstis)
{
Console.Write("Ja on tekstis olemas");
}
else
{
Console.Write("Ja-sid ei ole tekstis");
}
}
}
Console.ReadKey();
}
}
}
So This is my code and it isn't working the way it should. My teacher asked me to search for "ja" in text without contain method so we would think more logically. I completed all other exercises but this one. Thank you!
StackOverflow is actually not a place where people DO something for you.
They help you and tell you HOW to do this. This issue contains only the wrong piece of code and question "what's wrong".
First of all, I need to tell you that the first problem is, obviously, the algorithm.
I can't understand what is your code supposed to do because even you don't understand it.
using System;
using System.Text;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Where to search -->");
string text = Console.ReadLine();
string pattern = "ja"; // Probably, it is better to get it from Console as well
for (int i = 0; i < text.Length; i++)
{
for (int j = 0; j < pattern.Length; j++)
{
if (text[i+j] == pattern[j] && j == pattern.Length - 1)
Console.WriteLine(i);
if (text[i+j] != pattern[j]) break;
}
}
}
}
}
Here is the (not a best) code which searches for the pattern in the text without Contains method. It iterates through the text string - and if it meets the first character of pattern string - it goes further comparing characters one by one in a row. If the inner loop iterated till the end then it means that text string contains pattern string and outputs it's position. If in any moment characters are not equal then the inner loop breaks and continues the main loop.
Research it and understand it. Then you can solve the problem.
I have file in format DOC (MS Word 97-2003) and I want to get list of all images used in this file. I try to use "Microsoft.Office.Interop.Word" namespace like in code below
Application application = new Application();
Document document = application.Documents.Open(dataPath);
var words = document.InlineShapes;
int count = words.Count;
for (int i = 0; i < count; i++)
{
if (words[i] != null)
{
Console.WriteLine("{0} : {1}", i, words[i].PictureFormat);
}
}
but I can not find any image in this file (in real there exists two images). Maybe I do something wrong? Could you recommend me any library, which will easy it. I can'nt convert file to DOCX
Use document.InlineShapes to grab the images.
It may be funny, but in this case, I think, the numbering goes from 1. That's why you get COMException: "Element doesn't exists in collection".
Try:
for (int i = 1; i <= count; i++)
{
if (words[i] != null)
{
Console.WriteLine("{0} : {1}", i, words[i].PictureFormat);
}
}
i have done coding in C# but not much inside the Console App (teacher is making us do an assignment in it)
I have a problem where my static method works fine the first time it is called (each question is asked), but the second time through the console closes. I need this function to execute 10 times and not sure why it wont. Here is what i have and thanks in advance!:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab2
{
class Program
{
//Create the arrays
static string[] questions = new string[5]; //For questions
static int[] tableHeader = new int[10]; //Table Header
static int[,] responses = new int[5, 10]; //For answers
//Int for the number of times the questions have been asked
static int quizCount = 0;
static int answer;
static bool isGoing = true;
static void Main(string[] args)
{
//Set the questions in an array
questions[0] = "On a scale of 1-10, how do you feel about the drinking age in Wisconsin?";
questions[1] = "On a scale of 1-10, how often do you drink a week?";
questions[2] = "On a scale of 1-10, how important is this class?";
questions[3] = "On a scale of 1-10, how would you rate this campus?";
questions[4] = "On a scale of 1-10, how would you rate this command prompt?";
while(isGoing)
Questions();
}
static void Questions()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(questions[i]);
answer = Convert.ToInt16(Console.ReadLine());
responses[i, quizCount] = answer;
}
if (quizCount < 10)
{
Console.WriteLine("Enter more data? (1=yes, 0=no)");
int again = Console.Read();
if (again != 1)
Environment.Exit(0);
}
else
isGoing = false;
DisplayResults();
}
static void DisplayResults()
{
Console.WriteLine(tableHeader);
for (int i = 0; i < 5; i++)
{
for (int x = 0; x < 10; x++)
{
Console.Write(responses[i, x]);
}
Console.Write("\n");
}
}
}
}
First off Console.Read() returns an int representing the ascii value of what was entered. If the user enters 1, Console.Read() returns 49. (See this ascii table)
You could use Console.ReadKey()
Second, you need some fixes in the way you loop and ask to continue....
int again = Console.Read();
Your problem is here - Console.Read() returns the first character entered (as represented by its ASCII code), not the number you type in. I leave the solution for your homework.