How do i get the data to appear line after line in notepad
and with the dashes in the phone number

that my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Friends
{
    class WriteFriendRecord
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your friend's details: ");
            Friends.Friend f = new Friends.Friend();
            Console.Write("Frist Name: ");
            string fn = Console.ReadLine();
            Console.Write("Last Name: ");
            string ln = Console.ReadLine();
            Console.Write("Phone Number: ");
            long pn = Convert.ToInt64(Console.ReadLine());
            Console.Write("Month of the Birth: ");
            string mb = Console.ReadLine();
            Console.Write("Day of the Birth: ");
            int d = Convert.ToInt32(Console.ReadLine());
            f.setName(fn, ln);
            f.setPhoneNum(pn);
            f.setBirthDate(mb, d);

            System.IO.File.AppendAllText("F:/CSIS115c/WriteFriendRecord/Friends/bin/Debug/FriendsData.txt", f.ToString());
            Console.WriteLine("Record saved successfully");
            Console.WriteLine();
            Console.Read();
        }
    }
}

[B]Friend Class
[/B]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Friends
{
    class Friend
    {
        private string firstName;
        private string lastName;
        private long phoneNum;
        private string month;
        private int day;
        public Friend()
        {
        }
        public Friend(string f, string l, long num, string m, int d)
        {
            firstName = f;
            lastName = l;
            phoneNum = num;
            month = m;
            day = d;
        }
        public void setBirthDate(string m, int d)
        {
            month = m;
            day = d;
        }
        public void setName(string f, string l)
        {
            firstName = f;
            lastName = l;
        }
        public void setPhoneNum(long num)
        {
            phoneNum = num;
        }
        public void getFriend()
        {
            Console.WriteLine("First Name: " + firstName);
            Console.WriteLine("Last Name: " + lastName);
            Console.WriteLine("phone Number: " + phoneNum);
            Console.WriteLine("DateOfBirth: " + month + ", " + day);
        }
        public override string ToString()
        {
            return String.Format("{0} {1} {2} {3} {4}\n", firstName, lastName, phoneNum, month, day);
        }
    }
}

Recommended Answers

All 5 Replies

Pet peeve:

private long phoneNum;

A phone number is *not a number*. You do not add, subtract, multiply, divide or perform any math operation on it. It's a string with a limited character set. Treat it as such and your life will be much easier (think international calling, where leading zeros are significant).

As for your issue, you need to format the string. I wonder if there is anything that will String.Format

commented: Could not agree more! +14

Use \r\n for newline instead of \n

OR Use

Environment.NewLine;

Environment.NewLine will do the trick but to much headache. Instead of

writer.Write("this"+Environment.NewLine+"will" + Environment.NewLine + "make"+Environment.NewLine +"each" + Environment.NewLine + "word" + Environment.NewLine + "appear" + Environment.NewLine + "on" + Environment.NewLine + "a" + Environment.NewLine + "new" + Environment.NewLine+"line");

Instead try using \r\n like this

writer.Write("this\r\nwill\r\nmake\r\neach\r\nword\r\nappear\r\non\r\na\r\nnew\r\nline");

Instead of using NewLine property all the time, you can think of using StringBuiler, and append lines going through the loop of those words:

string[] array = {"this", "will","make"}; //and so on (put all the words into an array - or generic list<T>)
//then:
StringBuilder sb = new StringBuilder();
for(int i = 0; i< arrary.Lenght; i++)
      sb.AppendLine(array[i]);
writer.Write(sb.ToString());

Way shorted code, isnt it?

Pet peeve:

private long phoneNum;

A phone number is *not a number*. You do not add, subtract, multiply, divide or perform any math operation on it. It's a string with a limited character set. Treat it as such and your life will be much easier (think international calling, where leading zeros are significant).

As for your issue, you need to format the string. I wonder if there is anything that will String.Format

You are right a pnone number is a string and should be treated as nothing more unless converting to string[] which helps in getting the parts to the number like this

private string CountryCode, AreaCode, Prefix, Number;
        private string LongPhoneNum = "555-555-5555";
        private bool PhoneNumebrError;

        private void GetPhoneNumberParts()
        {
            string[] Numbers = LongPhoneNum.Split('-');
            if (Numbers.Length == 3)
            {
                // phone number is format 555-555-5555
                AreaCode = Numbers[0];
                Prefix = Numbers[1];
                Number = Numbers[2];
            }
            else if (Numbers.Length == 4)
            {
                // phone number is format 5-555-555-5555
                CountryCode = Numbers[0];
                AreaCode = Numbers[1];
                Prefix = Numbers[2];
                Number = Numbers[3];
            }
            else
            {
                PhoneNumebrError = true;
                // optional
                throw new NotSupportedException("Phone Number Was Not In A Supported Format");
            }
        }

Now just set the Phone Number to LongPhoneNum and use GetPhoneNumberParts(); to output the countrycdoe,areacode,prefix, and number. if an error occurs the bool phonenumbererror will be triggered and you can check if there was an error like this

if (!PhoneNumebrError)
{
using (StreamWriter sw = new StreamWriter(@"path\To\File\Here"))
{
// write phone data here
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.