Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Reading a text file in Java and printing backwards with recursion

java recursion lists loops list loop text-file printing

  • Please log in to reply
4 replies to this topic

#1 ibthevivin


    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:Java, PHP, JavaScript, Others

Posted 23 October 2012 - 12:07 AM

Write a Java program that recursively reads ten names from a file, and then outputs the total number of characters in the names, the list of names, and the list of names in reverse order. All looping must be performed recursively.

Jay Walker
Erol Flintstone
C. Erol Madre
Billy Pilgrim
Mickey Angels
José Francisco de San Martín
Squarebob Sponge Pants
Mischa Ternoff
Chester Peak
Al Italia
Ben Dover
Pat Pending

I am 100% lost. I would like advice on where would be the first place to start. Thinking about the program, I wanted to build a main that would call a scanner that would read the file first. When reading the file, it would count the characters in the text (quick question, would a scanner count the spaces between the characters?).

Next I was thinking of just a simple print function that would display the entire names.txt file.

Finally, the part that I'm 110% lost...how the heck would I go about listing the names in reverse order? What would I use? How does recursion fit in all this? O.o I'm totally blown at that part.
  • 0

#2 gaconga1105


    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 23 October 2012 - 12:43 AM

Please determine that you must recursively read the file or recursively output the names in reverse order
  • 0

#3 VNFox


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 23 October 2012 - 06:12 PM

hmm ... Did you take DataStructure class ? This is where the Stack coming ... first in last out ... :) remember? Try to apply it ... it should be easy.
  • 0

I don't just develop software. I find solutions to your business needs.

#4 ibthevivin


    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:Java, PHP, JavaScript, Others

Posted 30 October 2012 - 05:21 PM

UPDATE!! My code works! But...

My professor said this, "Most of what you have in main() should be in a run() method, and much of the initialization should be in a constructor."

Is what he said just a matter of cutting and moving code or do I have to start over again? Don't really understand what he said. O.o

The reason I ask that is cause I don't understand how constructors vs. methods work. I was taught constructor material in like a day, so I don't have a healthy grasp of it.


[color=#434343][font=helvetica, arial, verdana, 'ms sans serif', sans-serif]
import java.util.*;
import java.io.*;[/font][/color]
public class hw3
public static final int MAXLINES = 10;
public static void main(String[] args) throws FileNotFoundException
  Scanner scan = new Scanner(new File("names.txt"));
  String[] names = new String[MAXLINES];
  readNames(names, scan, 0);
  System.out.println(totalChars(names, 0,0));
static String[] readNames(String[] names, Scanner scan, int curLine)
  if(curLine >= MAXLINES)
   return names;
  names[curLine] = scan.nextLine();
   return readNames(names, scan, curLine+1);
static void forward(String[] names, int cur)
  if(cur >= names.length)
  forward(names, cur+1);
static void reverse(String[] names, int cur)
  if(cur >= names.length)
  reverse(names, cur+1);
static int totalChars(String[] names, int cur, int sum)
  if(cur >= names.length)
   return sum;
		 return totalChars(names, cur+1, sum+names[cur].length());

Sorry about the formatting of my code. Something must've happened when I copied it from jGrasp. O.o
  • 0

#5 Chall


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 30 October 2012 - 06:57 PM

It sounds like he wants you to improve the readability of your code by placing in the correct spots.
When he says to place the instantiation stuff in a constructor, either he means to have the variables actually instantiate in a constructor, or to make a builder where he meant constructor.
A constructor is a code block used by other clients (classes) to instantiate your class. From what I think he's trying to say, if you have all of your instantiations inside the main method, then the class will only work properly if and only if it is run by itself. If you put the instantiations inside the constructor, then any class can use your class properly. As it is, any client attempting to use your class is going to have to do a lot of typing on their part just to be able to use your class. You should make it more user friendly for other classes, and improve API flexability.

Also, when he says you need to put most of the material inside the main method into a run method, it's probably so that other clients can also access your class, and use the same variables that would have been used during execution by the JVM, or atleast that's what I presume he is trying to tell you.

It looks like it's just a matter of organizing and formatting your code. Shouldn't be that hard. :)

P.S. If my grammar is off, it's because I'm very tired at the moment.
  • 0
Speaks fluent Java

Also tagged with one or more of these keywords: java, recursion, lists, loops, list, loop, text-file, printing

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download