Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do relative file paths work in Eclipse?
    text
    copied!<p>So my 2009 new years resolution is to learn Java. I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse. Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.</p> <p>Here is the sample code:</p> <pre><code>import java.util.Scanner; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.GridLayout; class TeamFrame extends JFrame { public TeamFrame() throws IOException { PlayerPlus player; Scanner myScanner = new Scanner(new File("Hankees.txt")); for (int num = 1; num &lt;= 9; num++) { player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble()); myScanner.nextLine(); addPlayerInfo(player); } add(new JLabel()); add(new JLabel(" ------")); add(new JLabel("Team Batting Aberage:")); add(new JLabel(PlayerPlus.findTeamAverageString())); setTitle("The Hankees"); setLayout(new GridLayout(11,2)); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } void addPlayerInfo(PlayerPlus player) { add(new JLabel(player.getName())); add(new JLabel(player.getAverageString())); } } </code></pre> <p>And you can see in the below screen shot I have included this file.</p> <p><em>image no longer available</em></p> <p>Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.</p> <p>Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.</p> <pre><code>Scanner myScanner = new Scanner(new File("C:\\Hankees.txt")); </code></pre> <p>So basically, my question is what am I doing wrong? Or is Eclipse responsible for this in some way?</p> <p>Thanks for any and all help!</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