Author Topic: Help with creating C# program (learner)  (Read 2760 times)

0 Members and 1 Guest are viewing this topic.

lanks

  • Guest
Help with creating C# program (learner)
« on: July 09, 2007, 11:51:05 PM »
I am learning C# and ive decided to create a program to use what i learn along the way.

it needs to read a text file. take each line and split them into different strings. display one of the string which prompts the user for a response.

In each line in the file the variables arnt separated by commas or tabs but a spaces which can be any amount including 1.

I am using StreamReader to read the file and Substring but cant get it to split each line into a different arrays  (5) of strings, can someone give me a snipper of the loop most efficient to do this (including declaring variables)
« Last Edit: July 09, 2007, 11:52:27 PM by lanks »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Help with creating C# program (learner)
« Reply #1 on: July 10, 2007, 12:08:26 AM »

May help if you provide a sample of your data ..

how do you decide which line to use and which variables to use .. do you only use one predetermined line ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8858
  • AKA Daniel
Re: Help with creating C# program (learner)
« Reply #2 on: July 10, 2007, 01:23:12 AM »
Welcome lanks , maybe something in this code will give you some ideas   :-)

Code: [Select]
using System;
namespace PigLatin
{
  /// <summary>
  /// Pig Latin interpreter
  /// Demonstrate loops, string methods
  /// Andy Harris, 11/16/01
  /// </summary>
  class Pig
  {
    static void Main(string[] args)
    {
      string pigWord = "";
      string sentence = "";
      string firstLetter;
      string restOfWord;
      string vowels = "AEIOUaeiou";
      int letterPos;
      while (sentence.ToLower() != "quit")
      {
        Console.WriteLine("Please enter a sentence (or type \"quit\" to exit)");
        sentence = Console.ReadLine();
        foreach (string word in sentence.Split())
        {
          firstLetter = word.Substring(0, 1);
          restOfWord = word.Substring(1, word.Length - 1);
          letterPos = vowels.IndexOf(firstLetter);
          if (letterPos == -1)
          {
            pigWord = restOfWord + firstLetter + "ay";
          }
          else
          {
            pigWord = word + "way";
          }
          Console.Write("{0} ", pigWord);
        }
      }
    }
  }
}

Fatty

  • Guest
Re: Help with creating C# program (learner)
« Reply #3 on: July 10, 2007, 04:15:58 AM »
Hi,
I used this one, seems to worked good for me
Change string delimiter to youe suit, I used comma
separated text file

~'J'~

Code: [Select]
    Public Function ConvToArray(ByVal myList As ArrayList, ByVal bound As Integer) As String(,)
        Dim tmp As Array, n As Integer
        tmp = myList.ToArray
        Dim out(0 To (myList.Count / bound) - 1, bound - 1) As String
        For i As Integer = 0 To (myList.Count / bound) - 1
            For j As Integer = 0 To bound - 1
                out(i, j) = tmp(n)
                n += 1
            Next
        Next
        Return out
    End Function

    Public Function ReadFromFile(ByVal fn As String) As String(,)
        Dim fs As FileStream
        Dim txf As StreamReader
        Dim rdata As New ArrayList()
        Dim tmp As String() = Nothing

        Dim out As String(,)
        Dim s As String
        Dim itm As New ArrayList()
        rdata.Clear()
        itm.Clear()
        Dim ln As [Object]

        Try

            fs = New FileStream(fn, FileMode.Open, FileAccess.Read)
            txf = New StreamReader(fs)
            s = txf.ReadLine

            Do Until s Is Nothing

                tmp = Split(s, ",")

                For i As Integer = 0 To UBound(tmp)
                    ln = tmp(i)
                    itm.Add(ln)
                Next

                rdata.AddRange(tmp)

                s = txf.ReadLine

            Loop

            fs.Close()

            out = ConvToArray(rdata, UBound(tmp) + 1)

        Catch ex As Autodesk.AutoCAD.Runtime.Exception

            Throw New Autodesk.AutoCAD.Runtime.Exception("If the file existed it was closed")

        Finally

        End Try

        Return out

    End Function

Fatty

  • Guest
Re: Help with creating C# program (learner)
« Reply #4 on: July 10, 2007, 04:17:59 AM »

lanks

  • Guest
Re: Help with creating C# program (learner)
« Reply #5 on: July 12, 2007, 01:30:47 AM »
thank you for your help, much appreciated