logo CodeStepByStep logo

reverseChunksInFile

Language/Type: Java Scanner file input
Related Links:

Write a method named reverseChunksInFile that accepts a Scanner parameter representing an input file, where the file is filled with s/k word/integer pairs on each line, and reverses the chunks of each word, printing the words to the console. The "reversing of chunks" is as defined in the separate string problem reverseChunks found elsewhere in this site. (See that problem for details.) For example, if the file myinput.txt contains the following text:

MehranSahami 3 ChrisP 2
MartyStepp 4 KeithSchwarz 5 CynthiaBLee 3

NickT 1

Then the call of reverseChunksInFile(new Scanner(new File("myinput.txt"))); should print the following console output:

heMnarhaSima hCirPs 
traMetSypp htieKawhcSrz nyCihtLBaee 

NickT 

Notice that each word has its chunks reversed using the k value that immediately follows that word, such as the 3 after "MehranSahami" or the 5 after "KeithSchwarz". You must retain the line breaks that were present in the file originally.

Assumptions: You may assume that the given file exists, is readable, and follows the given format with an alternating pattern of words and integers. You may assume that the integers that follow each word are positive numbers. You may assume that each word on each line of the file is separated by a single space.

Constraints: In solving this problem, you should not create any data structures such as arrays. But you may create as many strings or Scanners as you like, and you may use as many simple variables (such as ints) as you like.

Note: You may want to go solve the string problem reverseChunks first and then paste its solution here so that you can call it as a helper to solve this problem. You can submit both methods in your solution, one after the other.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.