Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Here is the text file I must use and it must be wrote in this manner.

Grades.txt

Todd 33 94

Bill 82 69

Betty 89.5 89.5

I must use it this way, I can not rewrite it to were the data is on separate lines. Wish I could it would be easier to work with.

I am useing a label, A List Box A button.

This project creates a grade calculator.

I almost have it figured out but I still am having a snag.

Here is my code

Dim my As IO.StreamReader = IO.File.OpenText("a:\grades.txt")


   Private Sub btnGtGrades_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGtGrades.Click
       Dim fmtStr As String = "{0,-12}{1,-5}{2,-5}{3,-5}{4,-2}"
       Dim strStudent As String, strLetterGrade As String
       Dim Grade1, Grade2 As String
       Dim varEveryonesGrade As Double
       Dim varAverage As Double

       Dim Var1, Var2 As String
       
       strStudent = strStudent
       Var1 = "Grade1"
       Var2 = "Grade2"
       strStudent = my.ReadLine
       strStudent = strStudent.Substring(0, 12)
       Grade1 = Var1.Substring(1, 5)

       Grade2 = Var2.Substring(2, 5)

       varAverage = (Var1 + Var2) / 2

       If varAverage >= 90 Then
           strLetterGrade = "A"
       End If

       If varAverage >= 80 Then
           strLetterGrade = "B"
       End If
       If varAverage >= 70 Then
           strLetterGrade = "C"
       End If
       If varAverage >= 60 Then
           strLetterGrade = "D"
       End If
       If varAverage >= 50 Then
           strLetterGrade = "F"
       End If
       lstGtGrds.Items.Add(String.Format(fmtStr, Grade1, Grade2, varAverage, strLetterGrade))

The problem is with ripping the bits of info from the file.

Now my error is telling my that on the line

Grade2 = Var2.Substring(2, 5) that the info must be obtained within valid parameters? Then if whem that is fixed my next error with probable be my mathematical because my grades are no longer dimensioned as double.

I actually this I need to be able to rip the info from the data file have the lines Grade2 = Var2.Substring(2, 5) in a different manner. Because when I do my math (Grade1 + Grade2) / 2 ...

All the info should be dimed as double. Oh Yea then I have to display the class average in a textBox. I don't think that's gonna be a problem once I get past this.

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

I'm not sure of the syntax anymore because I stopped working with VB when C# came out, but this should get you pointed in the right direction (also some of these functions may be VB6 in which case you change those to reflect .NET architecture) , below are the changes I would make:

 

'Get rid of the Dim Grade1, Grade2 line and replace with:
Dim Grades() as String

'Set your strStudent this way:
strStudent = Trim(strStudent)

'Then get your grades:
Redim Grades() = Split(strStudent, " ")
'The students name is now in Grades(0),
'The first grade is in Grade(1),
'The second grade is in Grade(2),
'And if there were more grades just expand!

'For your average:
varAverage = (Convert.ToDouble(Grades(1)) + Convert.ToDouble(Grades(2))) / 2

 

Like I said, syntax may be off since it's been so long for VB for me, much less VB.NET - but this shouldn't definitely help you.

Posted

The split thing doesn't seem to work in vb.net

 

Here is the error message,,

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

 

Additional information: Index and length must refer to a location within the string.

 

It comes from these two lines... the first line is fine it is in the bottom two lines.

strStudent = lstGtGrds.Items.Add(strStudent.Substring(0, 12))

Grade1 = lstGtGrds.Items.Add(strStudent.Substring(12, 4))

Grade2 = lstGtGrds.Items.Add(strStudent.Substring(16, 4))

At first I was thinkin the first number was for the zone, but it is were to start counting from and the second is for how many spaces to count.

My zones are set up like

Dim fmtStr As String = "{0,-12}{1,-4}{2,-4}{3,-5}{4,-2}"

The listBox is set up with a true type font, to make my zones line up properly. My text file doesn't use a true type font. Does that matter?

Looking to expand my VB.Net knowledge.

 

Jblake00

  • Administrators
Posted

If the data in the first post is correct tstrings like 'Todd 33 94' are only 11 characters long, trying to do a substring from position 12 within that string will be past the end of the string - exactly like the error says.

When you say the split thing doesn't work - what happens? Do you get any results or just an error.

 

Also you may want to try the .Net equivilent of Split

 

Dim Grades() As String

'Set your strStudent this way:
strStudent = strStudent.Trim()

'Then get your grades:
Grades() =  strStudent.Split(" ")
'The students name is now in Grades(0),
'The first grade is in Grade(1),
'The second grade is in Grade(2),
'And if there were more grades just expand!

'For your average:
varAverage = (Convert.ToDouble(Grades(1)) + Convert.ToDouble(Grades(2))) / 2

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

The name in a section that is allotted 12 spaces the each grade is only two digits but is in a 4 space section. So the whole line is 20 spaces long.

The split thing may be simalar to .length

What happens is it puts a line under it in .net..Meaning that it is an invalid identifier. I think

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

Ok I got the first part, I have to reiniate the streamreader for each line.

The code is now.

strStudent = my.ReadLine
       strStudent = strStudent.Substring(0, 12)
       strStudent = my.ReadLine
       Grade1 = strStudent.Substring(12, 4)
       strStudent = my.ReadLine
       Grade2 = strStudent.Substring(16, 4)

Now I am down to the math error. Which is

"An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

 

Additional information: Cast from string "Grade1Grade2" to type 'Double' is not valid."

Looking to expand my VB.Net knowledge.

 

Jblake00

  • *Experts*
Posted

You have to convert the string to a double:

Grade1 = Double.Parse(strStudent.Substring(12, 4))
'OR
Grade1 = Convert.ToDouble(strStudent.Substring(12, 4))

Do not forget to enclose those in a Try Catch statement as if the string in not in a correct format, it will raise an error.

Posted

I tried your formula for the math and it produced this error

varAverage = (Convert.ToDouble(Grade1) + Convert.ToDouble(Grade2)) / 2

"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

 

Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list."

 

 

Now I think I will try something like

CDbl(grade1) + CDbl (grade2) /2 = varAverage

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

That worked out as varAverage = CDbl(grade1) = CDbl(grade2)

Now I have the worst kind of problem. A logical error. Err

Now the program runs but produces 7 output fields instead of the five I suspected.The code is usein all three lines in the text file.

It should only use the first line, but for some reason it used the last persons name and first to grades then addes the second persons grades and the produces the result of 126.5 percent the gives the letter grade of an F. ????

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted (edited)

Ok here is what I think may be the biggest problem. I have to reianiate the streamreader evertime I use it to get something.

So far I have done you useing strStudent = my.readline

then I get the data useing substring(0, 12)

the I reinaite the steam reader strStudent = my.readline

The I pull the data out by Grade1 = strStundent.SubString(12,4)

Maybe I should do Grade1 = trim(12,4)

And so on?

Any way I think that is what I will try.

Btw I am not ignoring anyones suggestion, but I kind of have a guideline I have to follow. Even though what you may suggest may be a better way of solving a problem I may not be able to use that info as of yet. But please put in what you think because it may be valueble later on and thanx for trying to help out.

One thing I am sure of I am infatuated with the idea of programing, I think about it day and night.

Edited by Jblake00

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

Although I was told that the second too lines of code was suppose to change I am thinking hard about trying mutants codes sniplets.

Ohh yea do yall know of any other posts of this nature?

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

Never mind I didn't know there was a whole topic on this subject, Could swore I posted this in general discusion.

Wow there are lots of people haveing problems with the stream reader.

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

Ok forget about this post.

I now can get the program to display info even if it isn't quite logical. I believe that one problem was with the way I had my stream reader dimensioned.

I had Dim my As IO.StreamReader = IO.File.OpenText("a:\grades.txt")

When I needed to have.

Dim my As IO.StreamReader
       my = IO.File.OpenText("A:\grades.txt")

Now this works.

       strStudent = strStudent
       strStudent = my.ReadLine
       strStudent = strStudent.Substring(0, 22)
       Grade1 = strStudent.Substring(12, 4)
       Grade2 = strStudent.Substring(18, 4)
       varAverage = (Grade1 + Grade2) / 2     

However I think that PlausiblyDamp was write about

"If the data in the first post is correct tstrings like 'Todd 33 94' are only 11 characters long, trying to do a substring from position 12 within that string will be past the end of the string - exactly like the error says."

I have unfornunately lost my orginal text file. So I can check it.

At least all I will have to is change a few numbers.

Looking to expand my VB.Net knowledge.

 

Jblake00

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...