I/O-TxtFile Line Distance Program | JAVA











up vote
0
down vote

favorite












I am trying to write a lab/program that loops through a .txt input file. Reads an entire line of data as a string, then splits that string into an array delimited by a space. If there are 4 items in the resulting array, and each item is a valid double, I want it to determine the distance between the points & return info to a separate output.txt file.



{CODE}



import java.io.*;
import java.util.Scanner;

public class RLabs {

public static void main(String args) throws FileNotFoundException {
double dealtaX, deltaY, distance,x1, y1, x2, y2;
String Line, item1, item2, item3, item4;
String Sect;
Scanner inFile;
PrintWriter outFile;

System.out.println("I/O File Line Distance Program Starting. Opening file...");
System.out.println("Please ensure data is entered per line in the format of (X1 Y1 X2 Y2).");
inFile = new Scanner(new File("input.txt"));
outFile = new PrintWriter(new File("countresults.txt"));


while(inFile.hasNext()){

Line = inFile.nextLine();
Sect = Line.split(" ");
if(Sect.length == 4) {
item1 = Sect[0];
item2 = Sect[1];
item3 = Sect[2];
item4 = Sect[3];

if(testDoubleLow(item1,0)&&(testDoubleLow(item2,0)&&(testDoubleLow(item3,0)&&(testDoubleLow(item4,0))))){
x1 = Double.parseDouble(item1);
y1 = Double.parseDouble(item2);
x2 = Double.parseDouble(item3);
y2 = Double.parseDouble(item4);
dealtaX = (x2-x1);
deltaY = (y2-y1);

distance = Math.sqrt(Math.pow(deltaY,2)+ Math.pow(dealtaX,2));
outFile.printf(Line);

outFile.printf(" The distance between (%1.1f,%1.1f) and (%1.1f,%1.1f) is %1.1fn", x1,y1,x2,y2,distance);

}
else {
outFile.println("Line did not correct data");
}






}
else {
outFile.println("Line did not have 4 item(s)");
}
}

outFile.close();
inFile.close();


System.out.println("Done...");
}

private static boolean testDoubleLow(String token, double l){

if(testDouble(token)) {
double num = Double.parseDouble(token);
if(num > l ) {
return true;
}
}

return false;
}

private static boolean testDouble(String s) {
try {
Double.parseDouble(s);
return true;
}
catch(Exception e) {
return false;


}

}
}


{INPUT DATA IM USING}



17.2 23.2 15.6 17.76



1



frog



123 frog 2 98



22 99 19 76



-13 03 92 03



l 123 03 96



1293.333333333333333 3 4 111111111111111111



2 1 1 2



21.4518345 94 21 mom



12 67 tti 30



128 30 -2 69



670 20 49 100



1230 230 304 500 60 30 60 606 59



{OUTPUT DATA AFTER PROGRAM IS RAN}



17.2 23.2 15.6 17.76 The distance between (17.2,23.2) and (15.6,17.8) is 5.7



Line did not have 4 item(s)



Line did not have 4 item(s)



Line did not have correct data



22 99 19 76 The distance between (22.0,99.0) and (19.0,76.0) is 23.2



Line did not have correct data



Line did not have correct data



1293.333333333333333 3 4 111111111111111111 The distance between (1293.3,3.0) and (4.0,111111111111111104.0) is 111111111111111104.0



2 1 1 2 The distance between (2.0,1.0) and (1.0,2.0) is 1.4



Line did not have correct data



Line did not have correct data



Line did not have correct data



670 20 49 100 The distance between (670.0,20.0) and (49.0,100.0) is 626.1



Line did not have 4 item(s)










share|improve this question







New contributor




Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I am trying to write a lab/program that loops through a .txt input file. Reads an entire line of data as a string, then splits that string into an array delimited by a space. If there are 4 items in the resulting array, and each item is a valid double, I want it to determine the distance between the points & return info to a separate output.txt file.



    {CODE}



    import java.io.*;
    import java.util.Scanner;

    public class RLabs {

    public static void main(String args) throws FileNotFoundException {
    double dealtaX, deltaY, distance,x1, y1, x2, y2;
    String Line, item1, item2, item3, item4;
    String Sect;
    Scanner inFile;
    PrintWriter outFile;

    System.out.println("I/O File Line Distance Program Starting. Opening file...");
    System.out.println("Please ensure data is entered per line in the format of (X1 Y1 X2 Y2).");
    inFile = new Scanner(new File("input.txt"));
    outFile = new PrintWriter(new File("countresults.txt"));


    while(inFile.hasNext()){

    Line = inFile.nextLine();
    Sect = Line.split(" ");
    if(Sect.length == 4) {
    item1 = Sect[0];
    item2 = Sect[1];
    item3 = Sect[2];
    item4 = Sect[3];

    if(testDoubleLow(item1,0)&&(testDoubleLow(item2,0)&&(testDoubleLow(item3,0)&&(testDoubleLow(item4,0))))){
    x1 = Double.parseDouble(item1);
    y1 = Double.parseDouble(item2);
    x2 = Double.parseDouble(item3);
    y2 = Double.parseDouble(item4);
    dealtaX = (x2-x1);
    deltaY = (y2-y1);

    distance = Math.sqrt(Math.pow(deltaY,2)+ Math.pow(dealtaX,2));
    outFile.printf(Line);

    outFile.printf(" The distance between (%1.1f,%1.1f) and (%1.1f,%1.1f) is %1.1fn", x1,y1,x2,y2,distance);

    }
    else {
    outFile.println("Line did not correct data");
    }






    }
    else {
    outFile.println("Line did not have 4 item(s)");
    }
    }

    outFile.close();
    inFile.close();


    System.out.println("Done...");
    }

    private static boolean testDoubleLow(String token, double l){

    if(testDouble(token)) {
    double num = Double.parseDouble(token);
    if(num > l ) {
    return true;
    }
    }

    return false;
    }

    private static boolean testDouble(String s) {
    try {
    Double.parseDouble(s);
    return true;
    }
    catch(Exception e) {
    return false;


    }

    }
    }


    {INPUT DATA IM USING}



    17.2 23.2 15.6 17.76



    1



    frog



    123 frog 2 98



    22 99 19 76



    -13 03 92 03



    l 123 03 96



    1293.333333333333333 3 4 111111111111111111



    2 1 1 2



    21.4518345 94 21 mom



    12 67 tti 30



    128 30 -2 69



    670 20 49 100



    1230 230 304 500 60 30 60 606 59



    {OUTPUT DATA AFTER PROGRAM IS RAN}



    17.2 23.2 15.6 17.76 The distance between (17.2,23.2) and (15.6,17.8) is 5.7



    Line did not have 4 item(s)



    Line did not have 4 item(s)



    Line did not have correct data



    22 99 19 76 The distance between (22.0,99.0) and (19.0,76.0) is 23.2



    Line did not have correct data



    Line did not have correct data



    1293.333333333333333 3 4 111111111111111111 The distance between (1293.3,3.0) and (4.0,111111111111111104.0) is 111111111111111104.0



    2 1 1 2 The distance between (2.0,1.0) and (1.0,2.0) is 1.4



    Line did not have correct data



    Line did not have correct data



    Line did not have correct data



    670 20 49 100 The distance between (670.0,20.0) and (49.0,100.0) is 626.1



    Line did not have 4 item(s)










    share|improve this question







    New contributor




    Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to write a lab/program that loops through a .txt input file. Reads an entire line of data as a string, then splits that string into an array delimited by a space. If there are 4 items in the resulting array, and each item is a valid double, I want it to determine the distance between the points & return info to a separate output.txt file.



      {CODE}



      import java.io.*;
      import java.util.Scanner;

      public class RLabs {

      public static void main(String args) throws FileNotFoundException {
      double dealtaX, deltaY, distance,x1, y1, x2, y2;
      String Line, item1, item2, item3, item4;
      String Sect;
      Scanner inFile;
      PrintWriter outFile;

      System.out.println("I/O File Line Distance Program Starting. Opening file...");
      System.out.println("Please ensure data is entered per line in the format of (X1 Y1 X2 Y2).");
      inFile = new Scanner(new File("input.txt"));
      outFile = new PrintWriter(new File("countresults.txt"));


      while(inFile.hasNext()){

      Line = inFile.nextLine();
      Sect = Line.split(" ");
      if(Sect.length == 4) {
      item1 = Sect[0];
      item2 = Sect[1];
      item3 = Sect[2];
      item4 = Sect[3];

      if(testDoubleLow(item1,0)&&(testDoubleLow(item2,0)&&(testDoubleLow(item3,0)&&(testDoubleLow(item4,0))))){
      x1 = Double.parseDouble(item1);
      y1 = Double.parseDouble(item2);
      x2 = Double.parseDouble(item3);
      y2 = Double.parseDouble(item4);
      dealtaX = (x2-x1);
      deltaY = (y2-y1);

      distance = Math.sqrt(Math.pow(deltaY,2)+ Math.pow(dealtaX,2));
      outFile.printf(Line);

      outFile.printf(" The distance between (%1.1f,%1.1f) and (%1.1f,%1.1f) is %1.1fn", x1,y1,x2,y2,distance);

      }
      else {
      outFile.println("Line did not correct data");
      }






      }
      else {
      outFile.println("Line did not have 4 item(s)");
      }
      }

      outFile.close();
      inFile.close();


      System.out.println("Done...");
      }

      private static boolean testDoubleLow(String token, double l){

      if(testDouble(token)) {
      double num = Double.parseDouble(token);
      if(num > l ) {
      return true;
      }
      }

      return false;
      }

      private static boolean testDouble(String s) {
      try {
      Double.parseDouble(s);
      return true;
      }
      catch(Exception e) {
      return false;


      }

      }
      }


      {INPUT DATA IM USING}



      17.2 23.2 15.6 17.76



      1



      frog



      123 frog 2 98



      22 99 19 76



      -13 03 92 03



      l 123 03 96



      1293.333333333333333 3 4 111111111111111111



      2 1 1 2



      21.4518345 94 21 mom



      12 67 tti 30



      128 30 -2 69



      670 20 49 100



      1230 230 304 500 60 30 60 606 59



      {OUTPUT DATA AFTER PROGRAM IS RAN}



      17.2 23.2 15.6 17.76 The distance between (17.2,23.2) and (15.6,17.8) is 5.7



      Line did not have 4 item(s)



      Line did not have 4 item(s)



      Line did not have correct data



      22 99 19 76 The distance between (22.0,99.0) and (19.0,76.0) is 23.2



      Line did not have correct data



      Line did not have correct data



      1293.333333333333333 3 4 111111111111111111 The distance between (1293.3,3.0) and (4.0,111111111111111104.0) is 111111111111111104.0



      2 1 1 2 The distance between (2.0,1.0) and (1.0,2.0) is 1.4



      Line did not have correct data



      Line did not have correct data



      Line did not have correct data



      670 20 49 100 The distance between (670.0,20.0) and (49.0,100.0) is 626.1



      Line did not have 4 item(s)










      share|improve this question







      New contributor




      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am trying to write a lab/program that loops through a .txt input file. Reads an entire line of data as a string, then splits that string into an array delimited by a space. If there are 4 items in the resulting array, and each item is a valid double, I want it to determine the distance between the points & return info to a separate output.txt file.



      {CODE}



      import java.io.*;
      import java.util.Scanner;

      public class RLabs {

      public static void main(String args) throws FileNotFoundException {
      double dealtaX, deltaY, distance,x1, y1, x2, y2;
      String Line, item1, item2, item3, item4;
      String Sect;
      Scanner inFile;
      PrintWriter outFile;

      System.out.println("I/O File Line Distance Program Starting. Opening file...");
      System.out.println("Please ensure data is entered per line in the format of (X1 Y1 X2 Y2).");
      inFile = new Scanner(new File("input.txt"));
      outFile = new PrintWriter(new File("countresults.txt"));


      while(inFile.hasNext()){

      Line = inFile.nextLine();
      Sect = Line.split(" ");
      if(Sect.length == 4) {
      item1 = Sect[0];
      item2 = Sect[1];
      item3 = Sect[2];
      item4 = Sect[3];

      if(testDoubleLow(item1,0)&&(testDoubleLow(item2,0)&&(testDoubleLow(item3,0)&&(testDoubleLow(item4,0))))){
      x1 = Double.parseDouble(item1);
      y1 = Double.parseDouble(item2);
      x2 = Double.parseDouble(item3);
      y2 = Double.parseDouble(item4);
      dealtaX = (x2-x1);
      deltaY = (y2-y1);

      distance = Math.sqrt(Math.pow(deltaY,2)+ Math.pow(dealtaX,2));
      outFile.printf(Line);

      outFile.printf(" The distance between (%1.1f,%1.1f) and (%1.1f,%1.1f) is %1.1fn", x1,y1,x2,y2,distance);

      }
      else {
      outFile.println("Line did not correct data");
      }






      }
      else {
      outFile.println("Line did not have 4 item(s)");
      }
      }

      outFile.close();
      inFile.close();


      System.out.println("Done...");
      }

      private static boolean testDoubleLow(String token, double l){

      if(testDouble(token)) {
      double num = Double.parseDouble(token);
      if(num > l ) {
      return true;
      }
      }

      return false;
      }

      private static boolean testDouble(String s) {
      try {
      Double.parseDouble(s);
      return true;
      }
      catch(Exception e) {
      return false;


      }

      }
      }


      {INPUT DATA IM USING}



      17.2 23.2 15.6 17.76



      1



      frog



      123 frog 2 98



      22 99 19 76



      -13 03 92 03



      l 123 03 96



      1293.333333333333333 3 4 111111111111111111



      2 1 1 2



      21.4518345 94 21 mom



      12 67 tti 30



      128 30 -2 69



      670 20 49 100



      1230 230 304 500 60 30 60 606 59



      {OUTPUT DATA AFTER PROGRAM IS RAN}



      17.2 23.2 15.6 17.76 The distance between (17.2,23.2) and (15.6,17.8) is 5.7



      Line did not have 4 item(s)



      Line did not have 4 item(s)



      Line did not have correct data



      22 99 19 76 The distance between (22.0,99.0) and (19.0,76.0) is 23.2



      Line did not have correct data



      Line did not have correct data



      1293.333333333333333 3 4 111111111111111111 The distance between (1293.3,3.0) and (4.0,111111111111111104.0) is 111111111111111104.0



      2 1 1 2 The distance between (2.0,1.0) and (1.0,2.0) is 1.4



      Line did not have correct data



      Line did not have correct data



      Line did not have correct data



      670 20 49 100 The distance between (670.0,20.0) and (49.0,100.0) is 626.1



      Line did not have 4 item(s)







      java strings array






      share|improve this question







      New contributor




      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 mins ago









      Brandon D. Arioso

      1




      1




      New contributor




      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Brandon D. Arioso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Brandon D. Arioso is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208900%2fi-o-txtfile-line-distance-program-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Brandon D. Arioso is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Brandon D. Arioso is a new contributor. Be nice, and check out our Code of Conduct.













          Brandon D. Arioso is a new contributor. Be nice, and check out our Code of Conduct.












          Brandon D. Arioso is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208900%2fi-o-txtfile-line-distance-program-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)