The Artima Developer Community
Sponsored Link

Java Answers Forum
Need Help Reading files

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris

Posts: 3
Nickname: topagae
Registered: Feb, 2003

Need Help Reading files Posted: Feb 6, 2003 8:52 AM
Reply to this message Reply
Advertisement
Making a applet that supposed to give a survey and store the results each time, but I am having trouble for it to read the file using "readInt", also problems writing to the data file.

code

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SurveyTest extends Applet implements ActionListener
{
DataInputStream input;
DataOutputStream output;
Label surveyNameLabel = new Label ("NHS Parental Survey");
Label getToWorkLabel = new Label ("Please Ansewer the Questions to the Best of Your Ability");

Checkbox q1 = new Checkbox("q1");
int q1Total=0;
Checkbox q2 = new Checkbox("q2");
int q2Total=0;
Checkbox q3 = new Checkbox("q3");
int q3Total=0;
Checkbox q4 = new Checkbox("q4");
int q4Total=0;
Checkbox q5 = new Checkbox("q5");
int q5Total=0;
Checkbox q6 = new Checkbox("q6");
int q6Total=0;
Checkbox q7 = new Checkbox("q7");
int q7Total=0;
Checkbox q8 = new Checkbox("q8");
int q8Total=0;
Checkbox q9 = new Checkbox("q9");
int q9Total=0;
Checkbox q10 = new Checkbox("q10");
int q10Total=0;
Button submitButton = new Button("Submit Results");
int counter=0;

public void init()
{
setBackground(Color.cyan);
add(surveyNameLabel);
add(getToWorkLabel);

setLayout(new GridLayout(11,1));
Panel panels[]=new Panel[11];
for(int i=0;i<panels.length;++i)
{
panels=new Panel();
panels.setLayout(new FlowLayout(FlowLayout.LEFT));
}
panels[0].add(q1);
panels[1].add(q2);
panels[2].add(q3);
panels[3].add(q4);
panels[4].add(q5);
panels[5].add(q6);
panels[6].add(q7);
panels[7].add(q8);
panels[8].add(q9);
panels[9].add(q10);
panels[10].add(submitButton);
submitButton.addActionListener(this);
for(int i=0;i<panels.length;i++)
{
add(panels);
}
//Open File to read data
try
{
input = new DataInputStream(new FileInputStream("Survey.dat"));
}
catch(IOException ex)
{
System.exit(1);
}
//read data
try
{
input.readInt(q1Total);
input.readInt(q2Total);
input.readInt(q3Total);
input.readInt(q4Total);
input.readInt(q5Total);
input.readInt(q6Total);
input.readInt(q7Total);
input.readInt(q8Total);
input.readInt(q9Total);
input.readInt(q10Total);
input.readInt(counter);
}
catch(IOException c)
{
System.exit(1);
}
}
//Do stuff at button's click
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
try
{
output = new DataOutputStream(new FileOutputStream("Survey.dat"));
}
catch(IOException op)
{
System.exit(1);
}
try
{
q1Total+=toDigit(q1);
q2Total+=toDigit(q2);
q3Total+=toDigit(q3);
q4Total+=toDigit(q4);
q5Total+=toDigit(q5);
q6Total+=toDigit(q6);
q7Total+=toDigit(q7);
q8Total+=toDigit(q8);
q9Total+=toDigit(q9);
q10Total+=toDigit(q10);
counter++;
}
catch(Exception cng)
{
System.exit(1);
}
//write data to files
try
{
output.writeInt(q1Total);
output.writeInt(q2Total);
output.writeInt(q3Total);
output.writeInt(q4Total);
output.writeInt(q5Total);
output.writeInt(q6Total);
output.writeInt(q7Total);
output.writeInt(q8Total);
output.writeInt(q9Total);
output.writeInt(q10Total);
output.writeInt(counter);
}
catch(IOException wt)
{
System.exit(1);
}
}
//change checkboxes to int
int toDigit(Checkbox ch)
{
boolean state = ch.getState();
if(state) return (1);
else return (0);
}
}

/code

Topic: Image resizing Previous Topic   Next Topic Topic: installing a software though java code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use