I need help getting the following code to compile- import java-io-File.docx
I need help getting the following code to compile- import java-io-File.docx
I need help getting the following code to compile- import java-io-File.docx
Nächste SlideShare
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Wird geladen in ... 3
1 von 3

Más contenido relacionado

Similar a I need help getting the following code to compile- import java-io-File.docx(20)

Más de patriciab30(20)

I need help getting the following code to compile- import java-io-File.docx

  1. I need help getting the following code to compile: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScrambleMain { public static void main(String[] args) { File file = new File("paragraph.txt"); String scrambledString = ""; try { Scanner sc = new Scanner(file); while (sc.hasNextLine()) { String line = sc.nextLine(); String lineArr[] = line.split(" "); System.out.println("input string :" + line); for (int i = 0; i < lineArr.length; i++) { String str = lineArr[i]; if (str.length() > 3) { scrambledString += scramble(str) + " "; } else { scrambledString += str + " "; } } } System.out.println("scrambledString : " + scrambledString); sc.close(); } catch (FileNotFoundException e) {
  2. e.printStackTrace(); } } public static String scramble(String str) { StringBuilder newStringBuilder = new StringBuilder(); char firstLetter = str.charAt(0); char lastLetter = str.charAt(str.length() - 1); StringBuilder stringBuilder = new StringBuilder(str.substring(1, str.length() - 1)); while (stringBuilder.length() > 0) { int n = (int) (Math.random() * stringBuilder.length()); newStringBuilder.append(stringBuilder.charAt(n)); stringBuilder.deleteCharAt(n); } return firstLetter + newStringBuilder.toString() + lastLetter; } } Solution The code works fine. Just give a proper input file with some words in it. The following is the result of running the program with the input file contains a word "computer" : output ---------------- input string :computer scrambledString : cmpeuotr