Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem here is only tangentially associated with knowing how to use modulus. You have one master function doing a mess of things: initializing variables, opening files, iterating through rows, and figuring out display parameters. This is known as procedural programming, and bad procedural programming at that: you want to leverage object oriented programming here.</p> <pre><code>//note the standards regarding class names: Capitalize Class Names! //also, pick names that make it clear what you're doing. public class DisplayTimes { DataInputStream in; //This is not actually the correct way to do this, but it's lightweight enough for this example List&lt;Integer&gt; runnerIds = new ArrayList&lt;Integer&gt;(); List&lt;Integer&gt; runnerMinutes = new ArrayList&lt;Integer&gt;(); List&lt;Integer&gt; runnerSeconds = new ArrayList&lt;Integer&gt;(); //note that your main method should not throw an exception. Nothing can catch it!! //also note that this is just an entry point; it probably shouldn't do any work public static void main(String[] args) { DisplayTimes display = new DisplayTimes("c:\\java\\chapter13\\sheet2\\program2.jdat2"); display.readAndDisplay(); } //constructors are the proper way to initialize object variables public DisplayTimes(String inputFile) { //see how much easier this next line is to read? this.in = new DataInputStream(new FileInputStream(fileName)); } public void readAndDisplay() { processData(); //offload processing somewhere else System.out.println("Runner\tTotal \tTotal \tTime"); System.out.println("Number\tMinutes\tSeconds\tBehind\n"); for (int idx = 0; idx++; idx&lt;runnerIds.size()) { String output = runnerIds.get(idx)+"\t"; output = output + runnerMinutes.get(idx)+"\t"; output = output + runnerSeconds.get(idx)+"\t"; output = output + calculateDifference(idx)+"\t"; System.out.println(output); } } private void processData() { boolean moreData = true; while (moreData) { //offload the work of the loop to another method moreData = processRunner(); } } private boolean processRunner() { try { int id=in.readInt(); in.readChar();//how is this file formatted that you need to skip a char? int mins=in.readInt(); in.readChar(); int secs=in.readInt(); //Here you should really sanity-check your values //Instead we'll just store them this.runnerIds.add(id); this.runnerMinutes(mins); this.runnerSeconds(secs); return isFinished(); } catch (EOFException e) { //Exceptions should NOT be used for program control. //They should be used when there is bad data. System.out.println("There was an unexpected termination of the datafile."); } } private boolean isFinished() { in.mark(1); if (in.read == -1) {//checks cleanly if we're at the end of the file. return false; } else { in.reset();//moves you back to mark return true; } } private String calculateDifference(int idx) { if (idx == 0) { return "\t"; } //First runner is first! int previousTimeInSeconds = (runnerMinutes.get(idx-1) * 60) + runnerSeconds.get(idx-1); int nextTimeInSeconds = (runnerMinutes.get(idx) * 60) + runnerSeconds.get(idx); int delta = (nextTimeInSeconds - previousTimeInSeconds); return (delta / 60) + "min " + (delta % 60) + "sec \t"; } } </code></pre> <p>The main thing you should take away here is that the problem you came here with - calculating the difference between runner a and b - is only a small part of the code you presented. You couldn't present a <a href="http://sscce.org/" rel="nofollow">Short, Self Contained, Correct (Compilable), Example</a> because your code was improperly tangled with other code. By writing clean code you can focus on the one function (<code>calculateDifference()</code>) that you actually needed help with. While I understand you may be beginning and working out of a book (based on what was presented), the most fundamental thing you can learn is how to break down a problem into the smallest pieces possible. Utilize the language to help you with this; don't fight it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload