|
Console.WriteLine("-----Enter at least 10 numbers-----");
int[] num = new int[10];
int input = 0;
while (input <= 10)
{
Console.Write("Enter a whole number: ");
num[input] = Convert.ToInt32(Console.ReadLine());
input++;
}
Console.Write("\nThis are the list of Even Numbers: ");
int Even = 0;
while (Even < 10)
{
if (num[input] % 2 == 0)
Console.Write(num[input]);
Even++;
}
Console.Write("\nThis are the list of your Odd Numbers: ");
int Odd = 0;
while (Odd < 10)
{
if (num[input] % 2 == 1)
Console.Write(num[input]);
Odd++;
}
Console.ReadLine();
|
|
|
|
|
num array has 10 elements with indices from 0 to 9.
Change the line
while (input <= 10) to
while (input < 10)
This you could have easily spotted if you had launched a debug session.
|
|
|
|
|
A debug session. Are you crazy? Then he would have to press F5. Much easier to post all the source code here, and press F5 to see if there is a repl... ohh.. wait. Never mind.
|
|
|
|
|
Phil.o is exactly right with what your problem is: array indexes in c# (and most modern languages) run from zero to N-1 where N is the number of elements. An attempt to use index N or any negative number is automatically outside the array and will generate a error.
But ... a couple of things:
0) It's a very good idea to always use curly brackets round even single lines inside an if :
if (num[input] % 2 == 0)
{
Console.Write(num[input]);
}
Even++;
If you don't, then you can get problems because the indentation is just to make teh code more readable to us, it means nothing to the compiler. If you add a line which is meant to be inside the if without curly brackets, it looks like it should work, but it won't:
if (num[input] % 2 == 0)
Console.Write(num[input]);
num[input] = 0;
Even++;
If you always use the brackets, it's obvious and you can't get it wrong:
if (num[input] % 2 == 0)
{
Console.Write(num[input]);
num[input] = 0;
}
Even++;
1) Don't comment what the instruction is doing:
int[] num = new int[10]; We know it's declaring an array, because we can read the code: it's declaring an integer array variable called num and assigning a new instance of 10 elements.
Your comment tells us nothing useful; it add nothing to the code. In fact it tells us less than the code does, and it's more likely to get "out of step" with the code if you change the array to a List (which is a kind of array that you can add and remove elements from - you'll meet them later).
Pretty much every comment in there does the same thing: document what the code is doing, not why you are doing it.
A better comment in this case is no comment at all, but use a different name for the variable:
int[] userInputs = new int[10]; That tells you what the array actually contains, without needing a comment.
This is called "self documentation" and it has the major advantage that it's much, much more likely to be kept up to date when code changes.
2) Don't use Convert to parse user inputs: users make mistakes, and Convert will cause your app to fail if they do. While this is only ten numbers, it's still going to be very annoying to type nine of them successfully and then miskey on the tenth and have the app crash out as a result!
Instead, use the TryParse methods:
int[] userInputs = new int[10];
int index = 0;
while (index < userInputs.Length)
{
Console.Write("Enter a whole number: ");
string input = Console.ReadLine();
if (int.TryParse(input, out userInputs[index]))
{
index++;
}
else
{
Console.WriteLine($"\"{input}\" is not an integer!");
}
}
3) Are you sure you want to use input in your final pair of loops?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 4-Jul-19 3:08am.
|
|
|
|
|
Do notice on your Odd and Even loops, the value
num[input] is always the same number as input variable is not modified for either loop. You may want
input[Even] input[Odd] for subsequent loops.
But I never wave bye bye
|
|
|
|
|
<pre>
using Microsoft.Office.Interop.Excel;
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelTestConsoleApp1
{
class Program
{
public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";
static void Main(string[] args)
{
CreateExcelFile();
ReadExcelFile();
Console.ReadLine();
}
private static void CreateExcelFile()
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
Console.WriteLine("Excel is not installed on this system... ");
return;
}
object misValue = System.Reflection.Missing.Value;
Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "FirstName";
xlWorkSheet.Cells[1, 2] = "LastName";
xlWorkSheet.Cells[2, 1] = "Greg";
xlWorkSheet.Cells[2, 2] = "Gonzales";
xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Excel file created successfully... ");
Console.BackgroundColor = ConsoleColor.Green;
}
private static void ReadExcelFile()
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\nReading the Excel File... ");
Console.BackgroundColor = ConsoleColor.Green;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
string firstValue, secondValue;
for (int rowCount = 1; rowCount <= totalRows; rowCount++)
{
firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text);
secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text);
Console.WriteLine(firstValue + "\t" + secondValue);
}
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Console.WriteLine("End of File... ");
}
private static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Console.WriteLine("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
Every time I try to run this code I get this error…
Exception Unhandled
System.Runtime.InteropServices.COMExceptions:'This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type.'
<pre>
|
|
|
|
|
At a guess? You have an old version of Excel installed.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Quote: xlExcel12 50 Excel Binary Workbook *.xlsb
public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";
XlFileFormat.xlExcel12,
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
|
hey all, beginner coder here! I have a project that I am going to attempt - I need to schedule an automated copy of files from several (10) client pcs to 1 main server. Any inputs on if I should use C# to build an executable that I could schedule on task scheduler to complete this? Or what would be the best to use? Thanks.
|
|
|
|
|
Yes: [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
hi, i have some problems to use httpexception but, in my visual studio httpexception not found, can anyone help my problem to handle httpexception please?
|
|
|
|
|
|
i have a question, does HttpTransportBindingElement have an exception? if there is a way to use the exception like what?
|
|
|
|
|
It's a class. Classes don't throw exceptions - properties and methods *IN* that class do. If you want to find out what execptions are thrown by the methods and properties in that class, intellisense will tell you, and so will google.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I am trying to converting the HTML string to PDF file using IText, It through the following exception
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
string htmltext = "my html string";
byte[] pdf = null;
var workStream = new MemoryStream();
using (var pdfWriter = new PdfWriter(workStream))
{
pdfWriter.SetCloseStream(false);
using (var document = HtmlConverter.ConvertToDocument(htmltext, pdfWriter))
{
pdf = workStream.GetBuffer();
}
}
workStream.Position = 0;
return pdf;
|
|
|
|
|
Assuming this is iTestSharp you are using, not IText:
string htmlText = File.ReadAllText(@"D:\Test Data\APage.html");
using (MemoryStream stream = new System.IO.MemoryStream())
{
using (StringReader sr = new StringReader(htmlText))
{
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f))
{
using (PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream))
{
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
File.WriteAllBytes(@"D:\Test Data\APage.pdf", stream.ToArray());
}
}
}
}
Provided your HTML is well formed, that should work fine.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Call it several times, remembering where you found it last...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
1. you could post your question on the CP article you cite. while it appears the author hasn't participated in several years: it's worth trying.
2. this post on the article's message thread [^] should give you some useful ideas.
3. given the possibility of recursion (nested images of depth > 1): you need to plan for that case.
Make some effort on your own, and revise/edit your post here, adding code, and specific questions.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
|
Voted as abuse, since the display of stupidity is and might infect others.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Yes ! Please respond with complete credit card details, including PIN, or, if you use PayPal your UserName and Password.
Your software can be ready in a matter of weeks, and will run on Win, Mac, Android, Linux, and any IOT device; it will be able to handle any language now in use in the world. Of course, we will provide you lifetime priority support at no extra charge.
Don't worry about more than one of us accessing your funds: we have an honor system here where the first transponder handles all that.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 30-Jun-19 1:06am.
|
|
|
|
|
No.
The only code you're ever going to get for this is the code YOU WRITE.
|
|
|
|
|