calling all java experts!!!

<p>public static double[] readFile(Scanner inFile)
{
double[] token = new double[8];
for(int i = 0; i < 8; i++)
{
while(inFile.hasNextDouble())
{
token* = inFile.nextDouble();
}
}
return token;
}</p>

<pre><code> public static void main(String[] args) throws IOException
{
File output= new File("output.txt");
Scanner inFile = new Scanner(output);
double[] token = readFile(inFile);

 for(int i = 0; i &lt; 8; i++)
 {
    System.out.println(token*);
 }

}
</code></pre>

<p>I have a file named "output.txt" that has the values:</p>

<p>1.1
2.2
3.3
4.4
5.5
6.6
7.7
8.8</p>

<p>And i want the program to read those numbers, then print them, but it's only printing the last number (8.8)!</p>

<ol>
<li>output.txt is misleading. That should be input.txt</li>
<li>It should be less than the array.length</li>
</ol>

<p>@MIT, it is less than array.length (I just used 8 instead of array.length) but, it’s still only reading the last value. If you run the program, the output is:</p>

<p>8.8
0.0
0.0
0.0
0.0
0.0
0.0
0.0</p>

<p>

What’s better than a loop? A nested loop! Especially if it screws up your program</p>

<p>It’s really fun to mentally go through a nested loop. Who the **** needs hot wheels when you do that to your brain</p>

<p>



public static double[] readFile(Scanner inFile)
{
    double[] token = new double[8];
    for(int i = 0; i < 8; i++)
    {
         while(inFile.hasNextDouble())    // YOUR PROBLEM. 
         {
              token* = inFile.nextDouble();
         }
    }
return token;
}


</p>

<p>Switch your while and for loops. Right now, it’s going:</p>

<p>for(int i=0, blah blah) {
bad while loop {
token[0] = 0.0
token[0] = 1.1
and so on…</p>

<p>So you end up with token[0] = 8.8. Since your .hasNextDouble() method goes through the entire input, it skips the while loop after proceeding for loops and leaves token[1]-token[7] with values of 0.0.</p>

<p>As a general rule of thumb, keep your for loops inside of your while loops. </p>

<p>This is what I think. I don’t remember much from Java since Java sucks as a language.</p>

<p>Easier fix:</p>

<p>Change while(inFile.hasNextDouble())
to if(inFile.hasNextDouble())</p>

<p>However, since your program already assumes that there are 8 inputs, it’s not necessary to have this there at all.</p>

<p>

</p>

<p>What language would you recommend learning after AP comp sci is done?</p>

<p>@FunStuff</p>

<p>I think people are too obsessed with “languages.” Java is as good as other languages as far as the basics go, and is considerably more robust than C++ and others. Learn about more datatypes and algorithms that the AP curriculum neglects (the AP doesn’t really cover any algorithms at all) before trying a new language</p>

<p>^ Okay, I will. Thanks. Anymore advice? (I’m gonna major in comp sci)</p>

<p>Depends on what your focus is in comp sci.</p>

<p>I’m specializing in the theory/more algorithm study part.</p>

<p>I’m going into the Air Force or Army as an officer which means I’ll probably end up with network security. What should I learn for that?</p>

<p>programming concepts and things that deal with networks and systems.</p>

<p>I would recommend trying a bunch of different things: web programming with a framework (Ruby on Rails for e.g.), databasing (SQL), parallel computing (C with MPI), and dataflow (LabVIEW for e.g.).</p>

<p>All radically different, but there’s a great chance you’ll run into at least 2-3 of them in the future.</p>

<p>LabView is such a fun yet easy language to use.</p>

<p>SQL’s a good one, and Ruby’s amazing.</p>