Solved

Writing numbers to a file C#

Posted on 2006-11-21
3
4,809 Views
Last Modified: 2008-02-01
Read all pairs (x, y) from in.txt, one in each line.
In each line of out.txt, write the list of all positive integers that are less than or equal to x and are dividable by y.
If y is equal to 0, write all the primes less than or equal to x.
If you do not find any positive integers, leave the line blank.

out.txt is the answer i need to get.

in.txt
7 2
20 4
6 9
18 0

out.txt
2 4 6
4 8 12 16 20

2 3 5 7 11 13 17


Here is what i have written.  For some reason it doesn't output the "Numers" method but only the "PrimeNumbers" Method.

using System;
using System.IO;

namespace PMTTEST1
{
      class Class1
      {
            static int number1;
            static int number2;
            static int num1;
            static int num2;

            [STAThread]
            static void Main(string[] args)
            {
                  ReadFile();
            }

            public static void ReadFile()
            {
                  string fileName = "input.txt";

                  StreamReader fileReader;
             
                  fileReader = new StreamReader(fileName);
                  string sLine;
      
                  sLine = fileReader.ReadLine();

                  string [] split;
                  const char delm = ' ';

                  while(sLine != null)
                  {
                        split = sLine.Split(delm);

                        number1 = int.Parse(split[0]);
                        number2 = int.Parse(split[1]);

                        if(number2 == 0)
                        {
                              PrimeNumbers();
                        }
                        else
                        {
                              
                              Numbers();
                        }
                        sLine = fileReader.ReadLine();
                  }
            }

            public static void Numbers()
            {
                  StreamWriter fileWriter = new StreamWriter("out.txt");

                  for(int i = 1; i <= number1; i++)
                  {      
                        num1 = i;
                        
                        if((num1 % number2) == 0)
                        {
                              fileWriter.Write(num1+ " ");
                        }
                  }
                  fileWriter.Close();
            }

            public static void PrimeNumbers()
            {
                  StreamWriter fileWriter = new StreamWriter("out.txt");

                  for(int j = 1; j <= number1; j++)
                  {      
                        num2 = j;
                        
                        if((Math.Pow(2, num2) % num2) == 2)
                        {
                              fileWriter.Write(num2+ " ");
                        }
                  }
                  fileWriter.Close();
            }
      }
}
0
Comment
Question by:jmkotman
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 17988934
Hi, you need to append to the out.txt not overwrite it each time (you can specify a boolean in the constructor for StreamWriter to append), and also you want a WriteLine before calling fileWriter.Close(), the following methods have the modifications:

            public static void Numbers()
            {
                  StreamWriter fileWriter = new StreamWriter("out.txt", true);

                  for(int i = 1; i <= number1; i++)
                  {    
                        num1 = i;
                   
                        if((num1 % number2) == 0)
                        {
                              fileWriter.Write(num1+ " ");
                        }
                  }
                  fileWriter.WriteLine();
                  fileWriter.Close();
            }

            public static void PrimeNumbers()
            {
                  StreamWriter fileWriter = new StreamWriter("out.txt", true);

                  for(int j = 1; j <= number1; j++)
                  {    
                        num2 = j;
                   
                        if((Math.Pow(2, num2) % num2) == 2)
                        {
                              fileWriter.Write(num2+ " ");
                        }
                  }

                  fileWriter.WriteLine();
                  fileWriter.Close();
            }
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17989012
obviously the program will continue appending to the file across multiple runs, you can delete the output file if it exists - on your program startup... so that the output only contains the results of the current run:

if (File.Exists("out.txt"))
    File.Delete("out.txt");
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17989049
You were very close.  I took the liberty of re-writing some of your code.  See if you understand why.-


using System;
using System.IO;

namespace PMTTEST1
{
      class Class1
      {
            static int number1;
            static int number2;
            static int num1;
            static int num2;
        static StreamWriter fileWriter;
            static StreamReader fileReader;

            [STAThread]
            static void Main(string[] args)
            {

                  string fileName = "input.txt";
                  fileReader = new StreamReader(fileName);
                  fileWriter = new StreamWriter("out.txt");

                  ReadFile();

                  fileWriter.Close();
            }

            public static void ReadFile()
            {
                                    
                  string sLine = fileReader.ReadLine();
     
                  string [] split;
                  const char delm = ' ';

                  while(sLine != null)
                  {
                        split = sLine.Split(delm);

                        number1 = int.Parse(split[0]);
                        number2 = int.Parse(split[1]);

                        if(number2 == 0)
                        {
                              PrimeNumbers();
                        }
                        else
                        {
                         
                              Numbers();
                        }
                        sLine = fileReader.ReadLine();
                  }
            }

            public static void Numbers()
            {
                  

                  for(int i = 1; i <= number1; i++)
                  {    
                        num1 = i;
                   
                        if((num1 % number2) == 0)
                        {
                              fileWriter.Write(num1+ " ");
                        }
                        
                  }
                  fileWriter.WriteLine();
            }

            public static void PrimeNumbers()
            {

                  for(int j = 1; j <= number1; j++)
                  {    
                        num2 = j;
                   
                        if((Math.Pow(2, num2) % num2) == 2)
                        {
                              fileWriter.Write(num2+ " ");
                        }
                  }
                  fileWriter.WriteLine();
                  
            }
      }
}


However, your PrimeNumbers procedure is not quite right.  It skips 2 (which is a Prime number) - see if you can figure out why it is skipping 2

AW
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pairs challenge 5 63
firstswap challenge 20 67
Adding Namespace to the second node of the xml 16 64
Advice on Xojo as a development tool over VB. 4 35
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now