Ok, I'm having the hardest time writing this code for work. Any suggestions on how to get me going in the right direction on this code would be great!!!

Project:
I need to make uninstalled fonts available for viewing for my co-workers. Therefore, I'm to write a code that can do this from the intranet site. First, I compiled a list of fonts on excel, csv, and text (tab delimited). Then I started to write the code in an applet that can ask the user to type in a font. Then the program is to spit out font(s) that are included in the users request or "no font available".

I got halfway there and then I somehow backtracked to this horrible code. Can anyone help me or maybe give me strong solid advice or a better direction on this coding?

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class DuckShowFonts extends Applet { 
	
	String fileToRead = "fonts.csv";
	String line;
	String repeat_mindlessly = "";
	String n;	
	String prHtml;
	int i;
	
	Graphics g;
	ArrayList<String> data;
	FileReader fr;
	BufferedReader br;
	StringBuffer strBuff;
	TextField typedFont;
	TextField typeface;
	
	Button findTheFont;
	Label qForFont;
	
	
	public void init()  { 
		setSize(800,200);
		
		qForFont = new Label("What font are you looking for? ");
        add(qForFont);
        typedFont = new TextField(20);
        add(typedFont);

	    g=getGraphics();
	    
	    findTheFont = new Button("Find Font");
	    add(findTheFont);
		
	    prHtml = this.getParameter(fileToRead);
	    if (prHtml != null) fileToRead = new String(prHtml);
	    
	    try {
			retreiveFile();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	    
	}
	
	public void retreiveFile() throws IOException {
		
	 
	    	fr = new FileReader(fileToRead);
	    	br = new BufferedReader(fr);
	    	data = new ArrayList<String>();
	    	//strBuff = new StringBuffer(); - do i need this too?
	        
	    	while((line = br.readLine()) != null){
	        	data.add(line);
	        	//strBuff.append(line + "\n"); - do i need this?

	        }
	    	for (i=0; i<data.size(); i++) {
				
		   		if (typedFont.equals(data.get(i))) {
					System.out.print(typedFont);
				}
		   		else{
		   			System.out.print("not in database");
		   		}
		   	}  
	        //txtArea.append(strBuff.toString());

	}  	
	
	public boolean action(Event e,Object o)
	   {
	         n = typedFont.getText();
	         //repaint(); - should i use this?
	         return true;
	   }

	public void paint(Graphics g){

		g.setColor(Color.black);
		g.drawString("Here are your choices "+n,10,100);
		//update(g);
	}
}

Do you have any specific questions (other than a general request for someone to write your code)?

Can you give us some pseudo code that shows what you want the logic of the program to be?

I'm not really looking for someone to specifically write my code - more like tell me what I'm doing wrong and giving me reasons on why this does not work. That would be much more helpful so I can continue learning java in a more productive manner. Please refer to the question above for reference.

psuedo code:

get applet to read file

ask user to input typeface
user input is compared to strings in file

output: typeface(s)
or
output: 'typeface n/a'

Thanks

The applet security model makes reading and writing pretty difficult. The applet runs in a glass box, basically, and it's a pretty hard job to get it to talk to the outside world.
If you just want to pass in data, that can be done, and you can pass out data after the file has run, but while the applet is in gear, it can't talk to anything, so your standard file io isn't going to work. (if you find a way to make it work, let me know, I know four guys who'll want to know about it)

So the way we handled this was by a perl CGI that used some javascript to pass parameters in to the applet when we were starting it up and out again when we finished. There may be other ways, but that's what we ended up doing.

What parts of the program are you having problems with?

Where do you write the output?

As jon.kiparsky said, applets require permission to read for anywhere EXCEPT from the location they were loaded from. So an applet loaded to a client's browser would NOT be able to read anything from the client's PC.

Edited 6 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.