Jump to content
  • Advertisement
Sign in to follow this  
Uberskull

A Java filename programming problem

This topic is 4898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This thing here is supposed to be an Java programming code for a panel which reads text from a file and converts it to a randomly generated picture. The thing is, I cant seem to be able to create the File object, as the compiler doesnt seem to recognise the class File which I find very strange indeed! Any suggestions? This is the error message: TextStringArtPanel.java:118: cannot find symbol symbol : class File location: class TextStringArtPanel fileScanner = new Scanner(new File (fileName)); Here is a sample of the code in question! *************************************************************** private int left, right, palindromeCounter = 0; //variables for the palindrome test private String fileName = "", currentText = "", totalText = "", signature = ""; private Scanner fileScanner; public TextStringArtPanel () { Random generator = new Random(); setBackground (Color.black); setPreferredSize (new Dimension(400, 300)); fileName = JOptionPane.showInputDialog("Input file name"); //Asks the user for the text file to use ****************************** ***This is the problem line***=>fileScanner = new Scanner(new File (fileName)); ****************************** while (fileScanner.hasNext()) { currentText = convertString(fileScanner.next()); left = 0; right = currentText.length() - 1; while (currentText.charAt(left) == currentText.charAt(right) && left < right) // The actual palindrome tester loop { left++; right--; } if (!(left < right)) palindromeCounter = palindromeCounter +1; totalText = totalText + currentText; } if (palindromeCounter > 0) signature = JOptionPane.showInputDialog(null, "Hurrey! " + palindromeCounter + " palindrome(s) detected! " + "Enter your name, oh great artist!"); } ***************************************************************** Thanks in advance! Edit: By the way, these are the classes I have already imported: import javax.swing.JPanel; import javax.swing.JOptionPane; import java.awt.*; import java.util.Random; import java.util.Scanner;

Share this post


Link to post
Share on other sites
Advertisement
Yup, tried to do that but got:

TextStringArtPanel.java:119: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
fileScanner = new Scanner(new File (fileName));


...thrown into my face!

Share this post


Link to post
Share on other sites
You need to handle the possible exception somewhere.

import java.io.*;

try
{
fileScanner = new Scanner(new File (fileName));
}
catch(FileNotFoundException e)
{
// complain about that the file wasn't found
}

Share this post


Link to post
Share on other sites
Whenever you want to create a File object, the method that uses it should have throws Exception after it. That way java will be happy and life goes on :D.
So for example:
public TextStringArtPanel() throws Exception {
..
..
..
}

and that should do it hopefully!
Good luck!

Share this post


Link to post
Share on other sites
Why exactly doesn't this work for me in the first place? I thought the File class was a part of the java.lang.* package and therefore automatically imported.

Share this post


Link to post
Share on other sites
The File class is not in java.lang.*, it is in java.io.* which are 2 seperate imports. You could think of them like directories if you wanted. The File class is in the io directory so looking for it in the lang directory is really a waste of time. I think the reason it isn't included is because not all programs use files therefore having file code in the program isn't effective. Also the java.lang has the basic functions for a java program. If I'm wrong, I'm sure someone will correct me :D.

Share this post


Link to post
Share on other sites
Ok then, after adding the throw Exception to the constructor as such:

public TextStringArtPanel () throws Exception
{
....
....
....
....
....
}

Do I also have to add it every time I use the constructor in another class?

I ask this because the driver class which uses the TextringArtPanel complains about this when I try to use the construnctor:


ArtDriver.java:54: unreported exception java.lang.Exception; must be caught or declared to be thrown
TextStringArtPanel panel = new TextStringArtPanel();



What should I do?

[Edited by - Uberskull on May 22, 2005 11:21:32 AM]

Share this post


Link to post
Share on other sites
If I have put the trows Exception statement in the TextStringArtPanel constructor like this:


public TextStringArtPanel () throws Exception


how do I make the constructor work in other classes? All I get when trying to compile a driver class which uses the TextStringArtPanel is:


ArtDriver.java:54: unreported exception java.lang.Exception; must be caught or declared to be thrown
TextStringArtPanel panel = new TextStringArtPanel();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!