Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static String[] readFromFile() throws IOException{
- Scanner in = new Scanner(System.in);
- String inputFileName;
- System.out.println("Enter a file name: ");
- inputFileName = in.nextLine();
- inputFileName = inputFileName + ".txt";
- FileInputStream inFile = new FileInputStream(inputFileName);
- byte[] line = new byte[inFile.available()];
- inFile.read(line);
- String text = new String(line);
- String[] words = text.split(" ");
- return words;
- }
- public static String largestWord_s(String words){
- int largestLength = 0;
- String largestWord = " ";
- for (String b:words.split(" ")){
- if (largestWord.length() == 0){
- largestLength = b.length();
- largestWord = b;
- } else {
- if (b.length() >= largestLength){
- largestLength = b.length();
- largestWord = b;
- }
- }
- }
- System.out.println("The longest word is :" + largestWord);
- System.out.println("The length of this word is:" + largestLength);
- return largestWord;
- }
- public static void writeToFile(String largestWord) throws IOException{
- Scanner in = new Scanner(System.in);
- int i;
- String newFileName;
- System.out.println("Enter file name for save:");
- newFileName = in.nextLine();
- newFileName = newFileName + ".txt";
- FileWriter fileWriter = new FileWriter(new File(newFileName));
- fileWriter.write("Самое длинное слово:" + largestWord);
- fileWriter.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment