-2

im trying to run a simulator and there are several problems, primarily.... -the code isn't printing out the values at the end of the program - the code does not actually create the file
-I'm pretty tired so forgive any foolish mistakes I made or details I have left out.

I've searched the website and I found this

What is the simplest way to write a text file in java

and

how to write to text file outside of netbeans

I thought i could edit code from the first link to work for me, but that did not work( whcih is what you see here)

the second page looks more simple but there's no surrounding code so im not sure what the context is and how I would implement it

import java.util.Random; 
import java.util.Scanner;

import java.io.*;
/*
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
*/



public class SimClass {  

    public static void main (String[] args) 
    {

        Scanner keyboard = new Scanner(System.in) ; 
        Random randomNumbers = new Random();  
        //create object from random class

        //create self explanaroty input parameters
        int pkt_in_q = 0 ;
        int pkt_dropped = 0;              
        int input  ;
        int output  ;
        int buffer  ; 
        int x ; 

        double y ;  
        //ask for values for buffer size. input rate, output rate

        y = randomNumbers.nextDouble();
        //attempt to assign a random number to the variable and 

        /*here you should get user input.
        buffer size, # of repitions , if 
        */



        //fix this 
        System.out.print("Enter an integer for the input rate ");
            input = keyboard.nextInt(); 


        System.out.print("Enter an integer for the output rate ");
            output = keyboard.nextInt(); 

        System.out.print("What is the buffer size ");
            buffer = keyboard.nextInt(); 




        for (x = 1000000; x >=0 ; x--)
        { /*
            simulate # of packets dropped/in the que,
            create file, write results to file, output results, 
            */        

            if (y > input/(output/buffer))
                { 
                 if (pkt_in_q < buffer)
                    {       
                        pkt_in_q++ ;
                    }
                 else 
                 {
                    pkt_dropped++ ;
                 } 
                 //if statement should terminate here 
                }
            else
             if (pkt_in_q > 0) 
             { 
                pkt_in_q -- ;     
             }


        }


        /*
            create file, write results to file, output results, 
            */  

        try { /*this seeems to be the problem, the program is either not doing
                anything with this or not making the results visible
                        */

            String content =( " pkt_in_q is " + pkt_in_q + 
                    "pkt_dropped is " + pkt_dropped);


                File file = new  File("C:/Temp/inputFile.txt");

                    // if file doesnt exists, then create it
                    if (!file.exists()) 
                    {
                    file.createNewFile();
                    }

                    FileWriter fw = new FileWriter(file.getAbsoluteFile());
                    try (BufferedWriter bw = new BufferedWriter(fw)) 
                    {
                    bw.write(content);
                    bw.close();
                    }

                    System.out.println("packets dropped value is = " +
                           pkt_dropped + "packets in q value is = " + pkt_in_q);

            } 
        catch (IOException e) 
        {
        //e.printStackTrace();
        }   

    }

}
Community
  • 1
  • 1
Anonymous
  • 11
  • 7
  • 3
    Have you tried _debugging_ your code? This would be more helpful than posting to Stack Overflow IMHO. – Tim Biegeleisen May 06 '16 at 11:06
  • 1
    Why are you suppressing exceptions? They will show you what is going wrong. – tak3shi May 06 '16 at 11:18
  • Tim I am using netbeans and the code is not throwing any errors, also the "debug" option next to the "run" button is greyed out, suggegsting the code is error free and my errors are logical or organizational. Takeshi, I am not sure what you are referring to. please elaborate. I would say i have about a 80 percent understanding of the code. above. like I said i coppied the code from elsewhere. – Anonymous May 06 '16 at 11:26
  • You need to learn about try, catch, exceptions and comments in code. e.printStackTrace() would display the error you have, but you are catching the exception and do not display anything. e.printStackTrace is commented out. – tak3shi May 06 '16 at 11:53

2 Answers2

0
public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    Random randomNumbers = new Random();
    //create object from random class

    //create self explanaroty input parameters
    int pkt_in_q = 0;
    int pkt_dropped = 0;
    int input;
    int output;
    int buffer;
    int x;

    double y;
    //ask for values for buffer size. input rate, output rate

    y = randomNumbers.nextDouble() * 10;
    System.out.println("Y++++++" + y);
    //attempt to assign a random number to the variable and 

    /*here you should get user input.
    buffer size, # of repitions , if 
     */

    //fix this 
    System.out.print("Enter an integer for the input rate ");
    input = keyboard.nextInt();

    System.out.print("Enter an integer for the output rate ");
    output = keyboard.nextInt();

    System.out.print("What is the buffer size ");
    buffer = keyboard.nextInt();

    for (x = 1000000; x >= 0; x--) { /*
                   simulate # of packets dropped/in the que,
                   create file, write results to file, output results, 
     */

        if (y > input / (output / buffer)) {
            if (pkt_in_q < buffer) {
                pkt_in_q++;
            } else {
                pkt_dropped++;
            }
            //if statement should terminate here 
        } else if (pkt_in_q > 0) {
            pkt_in_q--;
        }

    }

    /*
        create file, write results to file, output results, 
     */

    try { /*this seeems to be the problem, the program is either not doing
                   anything with this or not making the results visible
     */

        String content = (" pkt_in_q is " + pkt_in_q + "pkt_dropped is " + pkt_dropped);

        String folderPath = "D:" + File.separator + "Temp";
        String fileName = folderPath + File.separator + "inputFile.txt";
        File folder = new File(folderPath);
        File file = new File(fileName);
        folder.mkdir();
        // if file doesnt exists, then create it
        if (!file.exists()) {
            //file.mkdir();
            file.createNewFile();
            System.out.println("File created");

        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        try {
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("packets dropped value is = " + pkt_dropped
                + "packets in q value is = " + pkt_in_q);

    } catch (IOException e) {
        //e.printStackTrace();
    }

}

Corrected the code check this and change the file directory and folder directory according to your requirement.

Correction for details:

First you can't define a try block like below

try (BufferedWriter bw = new BufferedWriter(fw)) 

It should be defined like

try{
}
catch(IOException e)
{
    //do something
}

Modified code for that is like below:

FileWriter fw = new FileWriter(file.getAbsoluteFile());
                        BufferedWriter bw = new BufferedWriter(fw);
                        try 
                        {
                        bw.write(content);
                        bw.close();
                        }
                        catch (IOException e) 
                        {
                        //e.printStackTrace();
                        }

Second you have to create the directory first post that you can create the file inside the directory. so just modified the code to get the directory created and file created.

String folderPath = "D:" + File.separator + "Temp";
        String fileName = folderPath + File.separator + "inputFile.txt";
        File folder = new File(folderPath);
        File file = new File(fileName);
        folder.mkdir();         

Hope it clarifies your doubt.

Rishal
  • 1,480
  • 1
  • 11
  • 19
  • can you fully explain your changes and what I did wrong briefly please. i see several changes that were made and id like to be sure i understand what you did and why – Anonymous May 06 '16 at 12:05
  • "File.separator" is slash used in defining the file path. folder.mkdir() method is used to create the folder. – Rishal May 06 '16 at 12:05
  • well this is helpful, and the code runs, but the file is not createad after running the program within netbeans. Do i need to run from cmd to get the desired result? specifically id like to generate a file with the result of my computation. – Anonymous May 06 '16 at 12:24
  • It should create the directory, Please check if there is any configuration error or is there anything which is causing the problem.Try from command propmpt – Rishal May 06 '16 at 12:37
  • also i just noticed , the Y+++++ from the print statement is not appearing either – Anonymous May 06 '16 at 12:57
  • nor is the "file is created " , and even after creating a temp folder it remains empy. – Anonymous May 06 '16 at 13:04
  • well this things you have to debug at your end, As i have run this code and able to create folder and files successfully. – Rishal May 06 '16 at 14:19
0

I think Code not executing due to file path error.

File file = new  File("C:/Temp/inputFile.txt");

There is no folder called "Temp" in the C: drive. If u create Temp folder manually then the code will execute successfully.

File file = new  File("D:/Temp/inputFile.txt");

I have created "Temp" folder in D: drive and code executed successfully.

Leks
  • 77
  • 11