6 Replies - 997 Views - Last Post: 18 December 2010 - 04:29 PM Rate Topic: -----

#1 Guest_Ron burgundy*


Reputation:

int.Parse(list.max()) not working

Posted 17 December 2010 - 12:28 PM

I am trying to add a "," after each string in a list except for the last item. To do so I am creating two variables within the method to track the size of the list and to keep track of which item in the list the loop is on. I am getting a FormatException was unhandled "Inut string was not in a correct format."

Here's my code for the method.

public void CreateProblemsWrongList()
        {
            foreach (String i in problemsWrong)//Each string in the List is added to the variable
            {
                int test = int.Parse(problemsWrong.Max());
                int count = 0;
                if (count < test)
                {
                    outWrongAnswers += i + ", ";
                }
                count++;
            }
        }



All items added into the list are strings.

Is This A Good Question/Topic? 0

Replies To: int.Parse(list.max()) not working

#2 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: int.Parse(list.max()) not working

Posted 17 December 2010 - 12:38 PM

Are you sure all strings in problemsWrong are ints? Why not just use a List<int> if you are positive?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Ron Burgundy*


Reputation:

Re: int.Parse(list.max()) not working

Posted 17 December 2010 - 12:40 PM

Just realized im not using the list.max() correctly. How can I count how many string variables are in my list.
Was This Post Helpful? 0

#4 Guest_Ron Brugundy*


Reputation:

Re: int.Parse(list.max()) not working

Posted 17 December 2010 - 12:42 PM

@Robin
All the variables are strings that look like "randomnum+randomum" They aren't ints. I want to find the size of my list, since it will vary every time the program is run.
Was This Post Helpful? 0

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: int.Parse(list.max()) not working

Posted 17 December 2010 - 12:49 PM

problemsWrong.Count() will return the size.

There are two common ways to iterate a list. One is with the foreach like you use. The other is just a for loop. using the for loop, you would do this:
for(int i = 0; i < problemsWrong.Count(); i++)
   int myNum = int.Parse(problemsWrong[i]);
   /// more computations
}

If you don't want to use the last item in the list, change the escape statement.
for(int i = 0; i < problemsWrong.Count() - 1; i++)

This post has been edited by Robin19: 17 December 2010 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 Zunera  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 74
  • Joined: 07-December 10

Re: int.Parse(list.max()) not working

Posted 17 December 2010 - 01:15 PM

Hi, at the end of the day you are looking for this already existing function:
String.Join(", ", problemsWrong.ToArray());

Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: int.Parse(list.max()) not working

Posted 18 December 2010 - 04:29 PM

View PostRobin19, on 17 December 2010 - 06:49 PM, said:

for(int i = 0; i < problemsWrong.Count(); i++)
   int myNum = int.Parse(problemsWrong[i]);
   /// more computations
}


Would be better 'more efficient' if you assigned the size of the list to a variable instead of calling Count each time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1