klasscho

laba 3.1

Mar 3rd, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package com.company;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class Main {
  10.     public static String[] readFromFile() throws IOException{
  11.         Scanner in = new Scanner(System.in);
  12.         String inputFileName;
  13.         System.out.println("Enter a file name: ");
  14.         inputFileName = in.nextLine();
  15.         inputFileName = inputFileName + ".txt";
  16.         FileInputStream inFile = new FileInputStream(inputFileName);
  17.         byte[] line = new byte[inFile.available()];
  18.         inFile.read(line);
  19.         String text = new String(line);
  20.         String[] words = text.split(" ");
  21.         return words;
  22.     }
  23.  
  24.     public static String largestWord_s(String words){
  25.         int largestLength = 0;
  26.         String largestWord = " ";
  27.         for (String b:words.split(" ")){
  28.             if (largestWord.length() == 0){
  29.                 largestLength = b.length();
  30.                 largestWord = b;
  31.             } else {
  32.                 if (b.length() >= largestLength){
  33.                     largestLength = b.length();
  34.                     largestWord = b;
  35.                 }
  36.             }
  37.         }
  38.         System.out.println("The longest word is :" + largestWord);
  39.         System.out.println("The length of this word is:" + largestLength);
  40.         return largestWord;
  41.     }
  42.  
  43.     public static void writeToFile(String largestWord) throws IOException{
  44.         Scanner in = new Scanner(System.in);
  45.         int i;
  46.         String newFileName;
  47.         System.out.println("Enter file name for save:");
  48.         newFileName = in.nextLine();
  49.         newFileName = newFileName + ".txt";
  50.         FileWriter fileWriter = new FileWriter(new File(newFileName));
  51.         fileWriter.write("Самое длинное слово:" + largestWord);
  52.         fileWriter.flush();
  53.     }
  54.  
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment