Jump to content

Help with programming assignment?(java)

brownninja97

Hello basically my assignment is where i have to basically open two programs and basically write the first line from the first program and then copy the first line of code from the second program.

 

Then i copy the second line of code from the first and then the second and so on. once all lines from both programs is copied i put the results in a text file. 

 

Ive done everything so far, all i dont know how to do is how to take one line of code from one program and then a line from another program and vice versa.

 

Its due tomorrow so i need help asap.

package tuterial_1; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List; public class Tut10 {public static void main(String args[]) {String fileNameIn = "C:\\Users\\Vinay_second_stage\\workspace\\tuterial_1\\src\\tuterial_1\\Tut5.java";String fileNameInTwo = "C:\\Users\\Vinay_second_stage\\workspace\\tuterial_1\\src\\tuterial_1\\Tut3.java";/*System.out.println("Please enter a filename");fileNameIn = System.console().readLine();System.out.println("Please enter another filename");fileNameInTwo = System.console().readLine();*/ String fileNameout = "test";System.out.println("Please enter output filename");// fileNameout = System.console().readLine();Tut10 app = new Tut10();app.startApp(fileNameIn, fileNameout, fileNameInTwo); }public String[] readFile(String fileName){List <String> lines = new ArrayList <String> ();BufferedReader br = null; try {br = new BufferedReader(new FileReader(fileName));String line = "";while ((line= br.readLine())!=null){lines.add(line); } }   catch(IOException ioe){    System.out.println("Exception caught, exception is: "+ ioe.getMessage());       }finally{try{if (br != null){br.close();}}catch(IOException ioe){   }}String [] filedata = new String [lines.size()];for(int i=0; i< filedata.length; i++){filedata[i] = lines.get(i);} return filedata;}public void startApp(String fileNameIn,String fileNameOut,String fileNameInTwo){String [] filedata = readFile(fileNameIn);String [] filedatatwo = readFile(fileNameInTwo);System.out.println("file1 len = " + filedata.length + " file2 len = " + filedatatwo.length);BufferedWriter bw = null;try{bw = new BufferedWriter(new FileWriter(fileNameOut));int count = 1; for (String s:filedata){bw.write(count+s);bw.newLine();count++;   } for(String st:filedatatwo){bw.write(count+st);bw.newLine();count++;}bw.flush(); }catch(IOException ioe){System.out.println("Exception caught, exception is: "+ ioe.getMessage());}finally{try{if (bw != null){bw.close();}  }catch(IOException ioe){   }}}}

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

Read this very obvious announcement and then correct your post please. Until you have accomplished that, I for one shall not be helping you.

 

PZ20Fqo.png

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Read this very obvious announcement and then correct your post please. Until you have accomplished that, I for one shall not be helping you.

 

 

sorry im panicking so i rushed, not sure why it still comes out flat, my indents arent showing

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

sorry im panicking so i rushed, not sure why it still comes out flat, my indents arent showing

 

It would help to fix the indentation. Your specification is not at all clear to me either... perhaps you could revise it?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

sorry im panicking so i rushed, not sure why it still comes out flat, my indents arent showing

 

 

package tuterial_1;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Tut10 {

    public static void main(String args[]) {

        String fileNameIn = "C:\\Users\\Vinay_second_stage\\workspace\\tuterial_1\\src\\tuterial_1\\Tut5.java";

        String fileNameInTwo = "C:\\Users\\Vinay_second_stage\\workspace\\tuterial_1\\src\\tuterial_1\\Tut3.java";

        /*System.out.println("Please enter a filename");

        fileNameIn = System.console().readLine();

        System.out.println("Please enter another filename");

        fileNameInTwo = System.console().readLine();*/

                

        String fileNameout = "test";

        System.out.println("Please enter output filename");

    //    fileNameout = System.console().readLine();

        Tut10 app = new Tut10();

        app.startApp(fileNameIn, fileNameout, fileNameInTwo);

        

    }

    public String[] readFile(String fileName){

        List <String> lines = new ArrayList <String> ();

        BufferedReader br = null;

        try {

            br = new BufferedReader(new FileReader(fileName));

            String line = "";

            while ((line= br.readLine())!=null){

                lines.add(line);

                

            }

            

        }

     catch(IOException ioe){

         System.out.println("Exception caught, exception is: "+ ioe.getMessage());

         

     }

        finally{

            try{

                if (br != null){

                    br.close();

                }

            }

            catch(IOException ioe){

            

            }

        }

        String [] filedata = new String [lines.size()];

        for(int i=0; i< filedata.length; i++){

            filedata = lines.get(i);

        }

        

        return filedata;

    }

    public void startApp(String fileNameIn,String fileNameOut,String fileNameInTwo){

        String [] filedata = readFile(fileNameIn);

        String [] filedatatwo = readFile(fileNameInTwo);

        System.out.println("file1 len = " + filedata.length + " file2 len = " + filedatatwo.length);

        BufferedWriter bw = null;

        try{

            bw = new BufferedWriter(new FileWriter(fileNameOut));

            int count = 1;

                    

            for (String s:filedata){

                bw.write(count+s);

                bw.newLine();

                count++;

         }

            

            for(String st:filedatatwo){

                bw.write(count+st);

                bw.newLine();

                count++;

            }

            bw.flush();

            

        }

        catch(IOException ioe){

            System.out.println("Exception caught, exception is: "+ ioe.getMessage());

        }

        finally{

            try{

                if (bw != null){

                    bw.close();

                }

            

                

            }

            catch(IOException ioe){

            

            }

        }

    }

}

it works as a quote not sure why

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

It would help to fix the indentation. Your specification is not at all clear to me either... perhaps you could revise it?

its weird i know sorry, i have to call the code of two other programs, then copy one line of code from one program afterthat i copy one line of code from the second program, then the second line from the first program, once that is done the result is put in a text file. 

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

It would help to fix the indentation. Your specification is not at all clear to me either... perhaps you could revise it?

actually nvm. I've got it to work

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

its weird i know sorry, i have to call the code of two other programs, then copy one line of code from one program afterthat i copy one line of code from the second program, then the second line from the first program, once that is done the result is put in a text file. 

 

Ok, so simply read the source files as text into a couple of lists and iterate those. Alternate between them odd and even serializing the lines to your output file. Check that you don't run off the end of either as you go.

 

If I get some time I'll throw something simple together for you. I'm not near anything I can work with right now though. Maybe someone else will in the meantime.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

actually nvm. I've got it to work

 

Ok then perhaps I might offer a little clean up?

 

 

Main.java

Tut10.java

SomeFileA.txt

SomeFileB.txt

SomeFileC.txt - our output file

package com.someones.assignment;import java.io.IOException;public class Main{    public static void main(String[] args)    {        try        {            Tut10 app = new Tut10();            app.interleaveFiles("SomeFileA.txt", "SomeFileB.txt", "SomeFileC.txt");        }        catch (IOException e)        {            e.printStackTrace();        }    }}
package com.someones.assignment;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Tut10{    public void interleaveFiles(String filenameA, String filenameB, String outputFilename) throws IOException    {        List<String> fileA = Files.readAllLines(Paths.get(filenameA));        List<String> fileB = Files.readAllLines(Paths.get(filenameB));        if (fileA.size() <= 0 && fileB.size() <= 0)        {            return;        }        Files.write(Paths.get(outputFilename), interleave(fileA.iterator(), fileB.iterator()));    }    private List<String> interleave(Iterator<String> it1, Iterator<String> it2)    {        List<String> result = new ArrayList<>();        while (it1.hasNext() || it2.hasNext())        {            if (it1.hasNext())            {                result.add(it1.next());            }            if (it2.hasNext())            {                result.add(it2.next());            }        }        return result;    }}
In the sea without leesEating his wings variableWhen all his feathers be from him goneHere is now both white and redAll and some without fableUnderstand now well and rightThe bird of Hermes is my name eating my wings to make me tame.
Standeth the bird of HermesAnd maketh himself yet full stableHe standeth still here as a stoneAnd all so the stone to quicken the deadBoth hard and soft and malleableAnd thank you God of this sight
In the sea without leesStandeth the bird of HermesEating his wings variableAnd maketh himself yet full stableWhen all his feathers be from him goneHe standeth still here as a stoneHere is now both white and redAnd all so the stone to quicken the deadAll and some without fableBoth hard and soft and malleableUnderstand now well and rightAnd thank you God of this sightThe bird of Hermes is my name eating my wings to make me tame. 

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok then perhaps I might offer a little clean up?

 

 

Main.java

Tut10.java

SomeFileA.txt

SomeFileB.txt

SomeFileC.txt - our output file

thank you very much, this optimization helps a ton.

package com.someones.assignment;import java.io.IOException;public class Main{    public static void main(String[] args)    {        try        {            Tut10 app = new Tut10();            app.concatenateFiles("SomeFileA.txt", "SomeFileB.txt", "SomeFileC.txt");        }        catch (IOException e)        {            e.printStackTrace();        }    }}
package com.someones.assignment;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Tut10{    public void concatenateFiles(String filenameA, String filenameB, String outputFilename) throws IOException    {        List<String> fileA = Files.readAllLines(Paths.get(filenameA));        List<String> fileB = Files.readAllLines(Paths.get(filenameB));        if (fileA.size() <= 0 && fileB.size() <= 0)        {            return;        }        Files.write(Paths.get(outputFilename), interleave(fileA.iterator(), fileB.iterator()));    }    private List<String> interleave(Iterator<String> it1, Iterator<String> it2)    {        List<String> result = new ArrayList<>();        while (it1.hasNext() || it2.hasNext())        {            if (it1.hasNext())            {                result.add(it1.next());            }            if (it2.hasNext())            {                result.add(it2.next());            }        }        return result;    }}
In the sea without leesEating his wings variableWhen all his feathers be from him goneHere is now both white and redAll and some without fableUnderstand now well and rightThe bird of Hermes is my name eating my wings to make me tame.
Standeth the bird of HermesAnd maketh himself yet full stableHe standeth still here as a stoneAnd all so the stone to quicken the deadBoth hard and soft and malleableAnd thank you God of this sight
In the sea without leesStandeth the bird of HermesEating his wings variableAnd maketh himself yet full stableWhen all his feathers be from him goneHe standeth still here as a stoneHere is now both white and redAnd all so the stone to quicken the deadAll and some without fableBoth hard and soft and malleableUnderstand now well and rightAnd thank you God of this sightThe bird of Hermes is my name eating my wings to make me tame. 

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

thank you very much, this optimization helps a ton.

No problem, better to not use those buffered reader/writer classes when there's more eloquent and appropriate features to hand. Moreover I edited and changed the concatenateFiles call to interleaveFiles since that's what it actually does - we should always be accurate and declarative.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×