Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am tring to run my code on C#, visual studio 2015, and keep getting an error
the code im tring to run:

C#
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int len;
            string str="";
            while (str != "end")
            {
                len = str.Length;
                str.Remove(len - 1, 1);
                str.Remove(len - 2, 1);
                str.Remove(len - 3, 1);
                str.Remove(len - 4, 1);
                str.Remove(1, 1);
                str.Remove(2, 1);
                str.Remove(3, 1);
                str.Remove(4, 1);
               
                Console.WriteLine(str);
            }
        }
    }
}


can anyone tell me what is the problem?

What I have tried:

I havn't tried anything really, just tring to anderstand what i can do
Posted
Updated 25-Apr-16 13:44pm
v2
Comments
Patrice T 25-Apr-16 14:16pm    
And you plan to give us the error message ans position ?
Member 12484096 25-Apr-16 14:19pm    
its because it's an infinit loop, sorry for bothering u
Member 12484096 25-Apr-16 14:17pm    
ther is no error messege, the code just shuts sown the program

Besides the advice in the other Solutions about your empty string and the inevitable "Argument out of range" exception, you need to understand that strings are invariant, which means that once created, they are never modified.

This means the str.Remove(..., 1) does not change the string in str, it returns a new string with the specified characters removed. So you must assign this value to str to see the progressive changes:
C#
str = str.Remove(len - 1, 1);
str = str.Remove(len - 2, 1);
// etc. ...

So... what are you actually trying to accomplish? (Since there are plenty of other potential problems lurking in this code...)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 19:45pm    
5ed.
Perhaps one obvious key expression is missing here: "strings are immutable".
I did not pay enough attention for your answer while posting mine, Solution 6, but it can be considered as my 5 cents added. No, just 1 cent. :-)
—SA
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Inspect str as you run the program line by line.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 19:49pm    
Sorry: you are repeating this text many times; and it can be helpful in certain cases.
But in this case, this answer is totally irrelevant. There is nothing to debug. It's all about understanding of some basics — look at other answers.

You should not think that some valid advice copies here and where is good in all cases. There is nothing wrong with such copying, but perhaps you should think at each specific question a bit more and try to figure out what should be really useful. I suggest you take more selective approach when trying to copy your answer again and again.

—SA
Patrice T 25-Apr-16 21:41pm    
Hi SA,
Situation: The OP is here because of an "infinite loop" (see OP comment) and don't understand why.
My point of view: The program is build on expectations that don't match reality, but the OP don't understand where is the problem.
The debugger would show the why of "infinite loop" and that str.remove does nothing the way it is used.
I see it as a tool that help to spot which expectation do not match reality.
Sergey Alexandrovich Kryukov 25-Apr-16 22:12pm    
Yes, it makes certain sense. I would note though that even the debugger may not help the inquirer understand what's going on. This person may even fix the indices and then see that the string remains the same and just ask "how so?! I delete!" Wrong indices is one thing, but it's nothing compared with not understanding that the string is immutable and throwing out the return of a function. Can you see the point? We have the saying: "Having your head cut off, don't cry about the hair"...
—SA
Patrice T 25-Apr-16 22:41pm    
I see your point.
I consider that once the debugger gave hint about what is wrong, it is a lead to search how the thing really work.
Well...look at your code, and what you are asking it to do. Use teh debugger - it'll help you understand.
What do you have in str when you arrive at the loop?
Nothing - a blank (empty) string.
So the while test compares an empty string with a fixed string "end" and returns true because they are not the same, and it enters the loop.
So you get the length of the string. And because the string is empty, it contains no characters, so it's length is zero.
So then you tell the system to remove the character before the start of the string:
C#
str.Remove(len - 1, 1);
            0  - 1, 1
                -1, 1

And it goes "Do what? You can't remove characters from there!" and throws an exception. Probably, it throws an "Argument out of range" exception because it's trying to access the string as an array and all arrays in C# start with 0 - so any negative number is illegal as an array index.

What do you do to fix it? Well, you could check if the string is empty and not enter the loop - or you could read a string to process from somewhere and ensure that what you are trying to process will work...
 
Share this answer
 
v2
Quote:
can anyone tell me what is the problem?
Yes, the error message will tell you exactly the problem.

String.Remove requires a valid index. Since your string is empty, they will all fail. String.Remove Method (Int32, Int32) (System)[^]
 
Share this answer
 
Just one phrase should explain it all:

Strings are immutable.

You cannot modify any string after it is initialized. If you want to have a mutable variant of string, it's System.Text.StringBuilder.

Don't be confused by the methods like System.String.Substring. All string methods never modify any strings. Instead, the return a brand-new string instance. But in your code you totally ignore returned result — this is your main mistake.

—SA
 
Share this answer
 
If this is the exact code you are running, I'd expect that you are getting an ArgumentOutOfRangeException exception because str always equals an empty string "". str.Length is always going to equal 0 and str is never going to equal "end". There is no way this code could run as is.

str.Remove(len - 1, 1);
will always be called with the values
str.Remove(-1, 1);

And
[^]
says
ArgumentOutOfRangeException is thrown if
Either startIndex or count is less than zero.
-or-
startIndex plus count specify a position outside this instance.

I am not really sure what the code is supposed to do. ????
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900