خرید بک لینک

Vote count: 0

Okay, this program lets the user input a file location. ex. /Users/useame/Desktop/Words.txt. Its supposed to open the file after the user clicks submit, take whatever text is in it and sort the words in alphabetical order. Everything is works when I put in a correct file path but it does not sort it's contents and post the results in the window pane.

here is the code:

import java.io.*;
import java.util.*;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;

public class AlphabeticalOrder extends Application {

    public static void main(String[] args) {
        launch(args);
    }

   TextField fileName = new TextField();
   Button submitButton = new Button("Submit");
   TextArea outputArea = new TextArea();

   public void button() {
       submitButton.setOnAction(e -> {
System.out.println("button event");
           if(fileName.getText().length() > 0) {
               submitButtonclick();
           }
       });
   }

   public void filename(String[] file) throws IOException {
       // Check command-line parameter usage
       if (file.length > 0) {
           fileName.setText(file[0]);

           System.out.println("Source file: " + fileName.getText());
       }

       button();
   }

   @Override
   public void start(Stage primaryStage) {
       try {
           BorderPane root = new BorderPane();

           Scene scene = new Scene(root,400,400);
          // scene.getStylesheets().add(AlphabeticalOrder.class.getResource("/background.css").toExtealForm());

           primaryStage.show();

           // Set the stage title
           primaryStage.setTitle("AlphabeticalOrder");

           // Place the scene in the stage
           primaryStage.setScene(scene);

           // Display the stage
           primaryStage.show();

           // Hold a button in an HBox
           HBox inputBox = new HBox();
           inputBox.setSpacing(10);
           inputBox.setAlignment(Pos.CENTER);
           inputBox.getChildren().addAll(new Text("Filename:"));
           inputBox.getChildren().addAll(fileName);
           inputBox.getChildren().addAll(submitButton);
           root.setTop(inputBox);

           //This creates a text box to display the results
           //from reading the file in a pane
           outputArea.setStyle("-fx-text-fill: black");
           outputArea.setStyle("-fx-background-color: grey");
           root.setCenter(outputArea);
button();
       } catch(Exception e) {
           e.printStackTrace();
       }
   }


   public void submitButtonclick() {
        if(fileName.getText().length() > 0) {
            File sourceFile = new File(fileName.getText());
            if (!sourceFile.exists()) {
                System.out.println("Source file " + fileName.getText()
                    + " does not exist");
            }
            else
               System.out.println("Source file " + fileName.getText() + " open OK");
        }

        fileName.setText("Test 1");
    }


   class SortFile
   {
       //sorting function
   void sortArray(String array[]) {
       //Loop for no.of passes
   for(int i=0;i<array.length-1;i++)

       //Repeat no.of comparisons
       for(int j=0;j<array.length-i-1;j++)

           //Comparing adjacent elements
           if(array[j].compareTo(array[j+1])>0)
   {
               //Swap using temp variable
               String temp=array[j];
               array[j]=array[j+1];
               array[j+1]=temp;
              }
   }
   public void reader(String args[])
   {
       //Creating File object
       File freader;

       //Scaer for reading
       Scaer filescaer;

       //Array list for dynamic elements adding
   ArrayList <String> array = new ArrayList<String>();

   //If file name is no.of passed as argument is 1
   if(args.length==1)
   {
   try
   {
       //Create file object
       freader = new File(args[0]);

       //Reading from file
       filescaer = new Scaer(freader);

       //Reading until end of file
   while(filescaer.hasNext())
   {
       //Reading each word and to Array List
       array.add(filescaer.next());
   }
   }
   //If file IOException is thrown
      catch(IOException ie)
   {
   System.out.println(ie);
   }String[] newArray = new String[array.size()];

   //Convert Array list to ArraynewArray=array.toArray(newArray);

   System.out.println("List of strings from file Before Sorting : n");

   //Print before sorting words
   for(int i=0;i<newArray.length;i++)
   System.out.print(newArray[i]+" ");

   //Call sorting method
   sortArray(newArray);
   System.out.println("nList of strings from file After Sorting : n");

   //Print after sorting words
   for(int i=0;i<newArray.length;i++)
   System.out.print(newArray[i]+" ");

   System.out.print("n");
   }
   else
       //If file name is not passed as argument then show usage syntax
       System.out.println("Usage syntax: SoftFile <Words.txt>");}
   }
   }

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 6 ارديبهشت 1395 ساعت: 1:32

صفحه بندی