Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

read file in applet

Thread view: 
andrewtitus@alltel.net - 01 Mar 2006 21:52 GMT
Iam trying to read a text file into an applet. i took an example code for
application and trying to apply it to applet to see if I can get it to work
to learn how to do it but keep running into errors.

ÏURLDissector.java:15: init() in URLDissector cannot override init() in
java.applet.Applet; overriding method is static
ÏÏ§Ï   public static void init()
ÏÏ§Ï                      ^
ϼ§ÏURLDissector.java:17: non-static variable fileScan cannot be referenced
from a static context
ÏÏ§Ï     fileScan = new Scanner (new File("pictures.txt"));
ÏÏ§Ï     ^
ϼ§ÏURLDissector.java:18: non-static method readFile() cannot be referenced
from a static context
ÏÏ§Ï        readFile();
ÏÏ§Ï        ^
ÏϧÏ3 errors

Not sure how to fix this.

Andy

import java.util.Scanner;
import java.io.*;
import javax.swing.JApplet;

public class URLDissector extends JApplet
{

  String[] names;
  String url;
  Scanner fileScan, urlScan;
  int i = 0;
  //-----------------------------------------------------------------
  //  Reads urls from a file and prints their path components.
  //-----------------------------------------------------------------
  public static void init()
  {
    fileScan = new Scanner (new File("pictures.txt"));
       readFile();
           
  }
  public void readFile() throws IOException
    {
   
    // Read and process each line of the file
     while (fileScan.hasNext())
     {
        url = fileScan.nextLine();

        names[i] = url;
        i++;
    }
        //  Print each part of the url
        for(int j=0;j<names.length;j++)
           System.out.println (names[j]);

   
   
    }

}              
Oliver Wong - 01 Mar 2006 22:41 GMT
> Iam trying to read a text file into an applet. i took an example code for
> application and trying to apply it to applet to see if I can get it to
[quoted text clipped - 18 lines]
>
> Not sure how to fix this.

   Removing the keyword "static" from your method declaration.

   - Oliver
andrewtitus@alltel.net - 01 Mar 2006 22:59 GMT
It compiles now but still no display. Sorry for all the questions really
trying to learn this.

import java.util.Scanner;
import java.io.*;
import javax.swing.JApplet;
import java.awt.*;

public class URLDissector extends JApplet
{

  String[] names;
  String url;
  Scanner fileScan, urlScan;
  int i = 0;
  //-----------------------------------------------------------------
  //  Reads urls from a file and prints their path components.
  //-----------------------------------------------------------------
  public void init()
  {
    try{
       readFile();
     }
     catch (Exception IOexception){}
  }
public void readFile() throws IOException
    {
   
     fileScan = new Scanner (new File("pictures.txt"));
   

     // Read and process each line of the file
     while (fileScan.hasNext())
     {
         // url = fileScan.nextLine();
        names[i] = fileScan.nextLine();
            System.out.println (names[i]);
        i++;
        }
          
    }

}              
Oliver Wong - 02 Mar 2006 14:22 GMT
> It compiles now but still no display. Sorry for all the questions really
> trying to learn this.
[quoted text clipped - 38 lines]
>
> }

   It displays to the system console. If you're running it in a webbrowser,
you have to use the browser's "view console" feature to view the output. If
you want the output to display in the applet itself, you're going to have to
learn how to write a GUI, which isn't difficult, but it isn't trivial
either.

   If you've never done GUIs before, start off by writing normal
applications instead of applets at first. Applets will just add an extra
layer of difficulty while trying to learn GUIs.

   Read this tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/index.html

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.