Hwk - Outputting names in alphabetical order

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1189 Views - Last Post: 17 November 2013 - 09:18 AM Rate Topic: -----

#1 Misspelt  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 16-October 13

Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:03 PM

Guys I need to allow the user to input 3 names then these will be outputted in alphabetical order. I have successfully done so with 2 names but I can't seem to figure out how I can put in the 3rd name. I have made a dim name3 just as a start but I do no know where to proceed from here.

Sub Main()
        Dim name1 As String
        Dim name2 As String
        Dim name3 As String

        Console.Write("Please enter the first name: ") : name1 = Console.ReadLine
        Console.Write("Please enter the second name: ") : name2 = Console.ReadLine
        Console.Write("Please enter the third name: ") : name3 = Console.ReadLine

        If name1.CompareTo(name2) < 0 Then
            Console.WriteLine(name1 & " " & name2)
        End If

        If name1.CompareTo(name2) > 0 Then
            Console.WriteLine(name2 & " " & name1)

        End If



       

        Console.ReadLine()


Is This A Good Question/Topic? 0
  • +

Replies To: Hwk - Outputting names in alphabetical order

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,574
  • Joined: 12-June 08

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:09 PM

Perhaps use a collection and a loop? Say a for loop and bubble sort?
Was This Post Helpful? 0
  • +
  • -

#3 Misspelt  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 16-October 13

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:11 PM

View Postmodi123_1, on 16 October 2013 - 12:09 PM, said:

Perhaps use a collection and a loop? Say a for loop and bubble sort?

Oh sorry I didn't state it, but I literally just started Computing 2 weeks ago so I don't know what all these terms mean? And I don't think we have covered loop in class yet so is there any other way?
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 201
  • View blog
  • Posts: 1,257
  • Joined: 11-May 12

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:13 PM

If you use a List(Of T) there is a .Sort method implemented

you can research is here MSDN
Was This Post Helpful? 0
  • +
  • -

#5 Misspelt  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 16-October 13

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:16 PM

View Postybadragon, on 16 October 2013 - 12:13 PM, said:

If you use a List(Of T) there is a .Sort method implemented

you can research is here MSDN

Isn't there any way to finish off the code I started? I don't want to use these things as I don't actually know what they are
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,574
  • Joined: 12-June 08

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:19 PM

Okay.. well then start doing a compare by hand.. a string is what? An array. Compare one and two.. then compare two and three.. and then one and three..
Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 462
  • View blog
  • Posts: 3,161
  • Joined: 12-January 10

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:19 PM

View Postybadragon, on 16 October 2013 - 03:13 PM, said:

If you use a List(Of T) there is a .Sort method implemented

you can research is here MSDN



if she just started coding i doubt she has covered lists
Was This Post Helpful? 0
  • +
  • -

#8 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 201
  • View blog
  • Posts: 1,257
  • Joined: 11-May 12

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:23 PM

View PostDarenR, on 16 October 2013 - 03:19 PM, said:

if she just started coding i doubt she has covered lists


This is why I left a link to research, the way @OP would have to do it without a collection of some sort would be extremely inefficient, especially if the teacher says "now add another 3 names". In addition, it will teach @OP what they will be learning soon anyway. So why not get ahead of the game, and impress the teacher and classmates? ;)
Was This Post Helpful? 0
  • +
  • -

#9 Misspelt  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 16-October 13

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:28 PM

View Postybadragon, on 16 October 2013 - 12:23 PM, said:

View PostDarenR, on 16 October 2013 - 03:19 PM, said:

if she just started coding i doubt she has covered lists


This is why I left a link to research, the way @OP would have to do it without a collection of some sort would be extremely inefficient, especially if the teacher says "now add another 3 names". In addition, it will teach @OP what they will be learning soon anyway. So why not get ahead of the game, and impress the teacher and classmates? ;)/>


I don't exactly plan on impressing anyone at the moment. Don't understand how I should be able to do the thing you gave me the link to when I cannot do the basics. Anyway if I was to continue from where I started off, what else do I have to do?
Was This Post Helpful? 0
  • +
  • -

#10 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 462
  • View blog
  • Posts: 3,161
  • Joined: 12-January 10

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:28 PM

View Postybadragon, on 16 October 2013 - 03:23 PM, said:

. So why not get ahead of the game, and impress the teacher and classmates? ;)/>



because if it isnt in their assignment they will fail said assignment--- trust me it happened to me---

if they say they want arrays then you must use arrays

in her case it looks like they are working on if/else
Was This Post Helpful? 0
  • +
  • -

#11 Misspelt  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 16-October 13

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:31 PM

View Postmodi123_1, on 16 October 2013 - 12:19 PM, said:

Okay.. well then start doing a compare by hand.. a string is what? An array. Compare one and two.. then compare two and three.. and then one and three..

Lol sorry bear with me, I'm an absolute noob, I'm not exactly sure how to do what you just said. I thought you meant this: (I'm actually lost right now)
 If name1.CompareTo(name2) < 0 Then
            Console.WriteLine(name1 & " " & name2)
        End If


        If name1.CompareTo(name2) > 0 Then
            Console.WriteLine(name2 & " " & name1)

        End If



        If name2.CompareTo(name3) < 0 Then
            Console.WriteLine(name2 & " " & name3)
        End If


        If name2.CompareTo(name3) > 0 Then
            Console.WriteLine(name2 & " " & name3)

        End If



        If name1.CompareTo(name3) < 0 Then
            Console.WriteLine(name1 & " " & name3)
        End If


        If name1.CompareTo(name3) > 0 Then
            Console.WriteLine(name2 & " " & name3)

        End If


View PostDarenR, on 16 October 2013 - 12:28 PM, said:

View Postybadragon, on 16 October 2013 - 03:23 PM, said:

. So why not get ahead of the game, and impress the teacher and classmates? ;)/>/>



because if it isnt in their assignment they will fail said assignment--- trust me it happened to me---

if they say they want arrays then you must use arrays

in her case it looks like they are working on if/else


Oh sorry if I didn't make it clear, and you are right, the current topic I am working on is IF Statements, so please can I have help solving this using IF Statements and not all these other things
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,574
  • Joined: 12-June 08

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:34 PM

You were more or less on the right track by using the "String.Compare Method".

Quote

Compares two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.

Value


Condition

Less than zero strA is less than strB.

Zero strA equals strB.

Greater than zero strA is greater than strB.
http://msdn.microsof...y/zkcaxw5y.aspx

Was This Post Helpful? 0
  • +
  • -

#13 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 462
  • View blog
  • Posts: 3,161
  • Joined: 12-January 10

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:38 PM

you might want to try a nested if statement

if (a>b)then
do something
else

          if (b>c) then
do something
else
if (c>a) then
do something
else
              if (b>a) then
do something
else
if (c>B)/> then
do something

else
do something


this is just to show you how certain if's work not a working if statement
Was This Post Helpful? 0
  • +
  • -

#14 Misspelt  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 16-October 13

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:40 PM

View Postmodi123_1, on 16 October 2013 - 12:34 PM, said:

You were more or less on the right track by using the "String.Compare Method".

Quote

Compares two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.

Value


Condition

Less than zero strA is less than strB.

Zero strA equals strB.

Greater than zero strA is greater than strB.
http://msdn.microsof...y/zkcaxw5y.aspx

Oh yeah I used that idea but the problem I have is the fact that using that ^^ I was able to make the output in alphabetical order when there are 2 inputs, now I'm trying to do the same for when there are 3 inputs but I don't exactly know what I am doing

View PostDarenR, on 16 October 2013 - 12:38 PM, said:

you might want to try a nested if statement

if (a>b)then
do something
else

          if (b>c) then
do something
else
if (c>a) then
do something
else
              if (b>a) then
do something
else
if (c>B)/>/> then
do something

else
do something


this is just to show you how certain if's work not a working if statement

Oh cool, my worksheet asks to use the nested if statement as well. I will try this out and see if I get any results
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,574
  • Joined: 12-June 08

Re: Hwk - Outputting names in alphabetical order

Posted 16 October 2013 - 12:41 PM

Then use multiple if statements like Daren said.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3