LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 300 users online 221739 members 327 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
Rollo Tomasi
sorry about my face guys
Mood: Lifeless
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
3 online / 19 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

Java FileI/O
Replies: 5Last Post Mar. 11, 2005 2:06pm by squirellplayingtag
Welcome to LiveWire!
We're Stronger Together.
Join the Community
Single page for this topic Email Print Favorite
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
For an assignment we have to read in a text file, add some numbers to certain numbers, then put it back in the text file. This isn't really important to my question, but may help you to understand.

I can read in the text file just fine, but when I go to put the changed info back into it, nothing is added. Nothing at all. It's rather bother some. Is it because you can't read and write to the same file in the same method?

Important Code:
Code:

myFile.readLine();
  for(int a = 0; a < wrongAnswersArray.length; a++){
  String nextLine = myFile.readLine();
  int dotLocation = nextLine.indexOf(":");
  int value = Integer.parseInt(nextLine.substring(dotLocation+1));
  wrongAnswersArray[a] = value;
  //System.out.println(value);
 
  }
 
  for(int b = 0; b < wrongAnswers.size(); b++){
  Integer wrapper = (Integer)wrongAnswers.get(b);
  wrongAnswersArray[wrapper.intValue()-1]++;  
  }
 
  myFile.readLine();
  //myFile.readLine();
  for(int c = 0; c < 10; c++){
 
  String nextLine = myFile.readLine();
  holder[c] = nextLine;
  //System.out.println("test");
 
  }
 
  myFile.close();
 
 
  myFileOut.println("questionsandanswers.txt");
  for(int d = 0; d < wrongAnswersArray.length; d++){
  myFileOut.println(d+1 + ":" + wrongAnswersArray[d]);
  }
 
  myFileOut.println("answersandquestions.txt");
  myFileOut.println("");
 
  for(int e = 0; e < 10; e++){
  myFileOut.println(holder[e]);
  }


-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler


1:40 pm on Mar. 11, 2005 | Joined: Jan. 2003 | Days Active: 1,260
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | Posts: 6,458 | Points: 27,128
LiveWire Humor
firemouth


Dairy Product Addict

Patron
Reply
can we see the whole program?

this isnt enough

-------
Science: "Why does this work?"
Engineering: "How does this work?"
Management: "When will this work?"
Liberal Arts: "Would you like fries with that?"


1:44 pm on Mar. 11, 2005 | Joined: July 2003 | Days Active: 215
Join to learn more about firemouth Michigan, United States | Straight Male | Posts: 1,095 | Points: 5,319
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
Code:

/**
  @Author: Dave O'Brien(Mostly me because I own) & Zach Booth
     Date: Mar 01, 2005
  Teacher: Mr. Dissinger
*/
import apcslib.*; // formatting / Drawing tools
import chn.util.*; // Input Console - ConsoleIO input = new ConsoleIO();
import java.util.*; //Random number; array lists

public class TestQuestions  {


static ArrayList myQuestion = new ArrayList();
static ArrayList wrongAnswers = new ArrayList();
static Random num = new Random();
static int numProblems = 0, numCorrect = 0, countR = 0, testNumber;
static Questions currentQuestion;
static String quesAns;

public static void main(String args[]) {
ConsoleIO input = new ConsoleIO();
System.out.print("Enter user name:");
String userName = input.readLine();

System.out.println("Enter Test Number, 1 or 2:");

testNumber = input.readInt();
while((testNumber != 1) && (testNumber != 2))
{
System.out.print("Not a valid test number, choice again:");
testNumber = input.readInt();
}

String test =  null;
switch(testNumber){
case 1:
test = "questionsandanswers.txt";
break;
case 2:
test = "answersandquestions.txt";
break;
}
System.out.print("How many questions do you want (more than 10 = 10)?");
int numOfQuestions = input.readInt();
readInQuestions(test);
System.out.println();

while(countR != numOfQuestions && countR <10){
//System.out.println(myQuestion.size());
System.out.println("\n**********************************************\n");
choiceNextProblem();
System.out.println(currentQuestion.getQuestion());
System.out.println(currentQuestion.mixThemUp());
//System.out.println(currentQuestion.getAnswer());
char answer = (char)(input.readToken().charAt(0));
switch(answer){
case 'a': answer = 'A';
break;
case 'b': answer = 'B';
break;
case 'c': answer = 'C';
break;
case 'd': answer = 'D';
break;
case 'e': answer = 'E';
break;
default: break;
}

while(answer > 'F'){
System.out.print("Invalid answer, reenter.");
answer = (char)(input.readToken().charAt(0));

switch(answer){
case 'a': answer = 'A';
break;
case 'b': answer = 'B';
break;
case 'c': answer = 'C';
break;
case 'd': answer = 'D';
break;
case 'e': answer = 'E';
break;
default: break;
}
}

if(currentQuestion.getAnswer() == answer)
numCorrect++;
else
wrongAnswers.add(new Integer(currentQuestion.getId()));

numProblems++;
countR++;
}
//System.out.println(currentQuestion.getAnswer() + " " + answer);
Calendar cal = new GregorianCalendar();


int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);

System.out.println(numCorrect + "/" + numProblems);
System.out.println(((double)numCorrect / numProblems) * 100 + "%");
writeInfo(userName, numCorrect, numProblems, hour, min, wrongAnswers, test);
reportWrong(wrongAnswers);
}

public static void readInQuestions(String file){
FileInput inFile;
String fileName = file;
inFile = new FileInput(fileName);
String ans, aAns, bAns, cAns, dAns, eAns;
String questString = "";
int x = 0;
boolean questionTest;

while(inFile.hasMoreLines()){
questionTest = true;
questString = "";
if(((char)(inFile.readLine()).charAt(0)) == '$'){
questString += inFile.readLine();

while (questionTest){
String tempReadIn = inFile.readLine();

if (((char)tempReadIn.charAt(0))!= '@')
{
questString += "\n" + tempReadIn;
questionTest = true;
}

else
{
questionTest = false;
}
}

}
int id = inFile.readInt();
ans = inFile.readLine();
aAns = ans;
bAns = inFile.readLine();
cAns = inFile.readLine();
dAns = inFile.readLine();
eAns = inFile.readLine();
myQuestion.add(new Questions(id, questString, ans, aAns, bAns, cAns, dAns, eAns));
}
}

public static void choiceNextProblem(){

int nextNumber = num.nextInt(myQuestion.size());
currentQuestion = (Questions)myQuestion.get(nextNumber);
myQuestion.remove(nextNumber);
}

public static void writeInfo(String name, int correct, int probs, int hour, int min, ArrayList wronged, String test){
String fileName = "userInfo.txt";
FileOutput outFile = new FileOutput(fileName,"append");
outFile.println("Name: " + name);
outFile.println("Test Name: " + test);
outFile.println("Questions wronged: "+ wronged);
outFile.println("Number Correct: " + correct);
outFile.println("Number of Problems: " + probs);
outFile.println("Time: " + hour + ":" + min);
//outFile.println(report);
outFile.println();
outFile.close();
}

public static void reportWrong(ArrayList wrongAnswers){
FileInput myFile = new FileInput("testResults.txt");

int[] wrongAnswersArray = new int[10];
String [] holder = new String[10];
switch(testNumber){
case 1:myFile.readLine();
  for(int a = 0; a < wrongAnswersArray.length; a++){
  String nextLine = myFile.readLine();
  int dotLocation = nextLine.indexOf(":");
  int value = Integer.parseInt(nextLine.substring(dotLocation+1));
  wrongAnswersArray[a] = value;
  //System.out.println(value);
 
  }
 
  for(int b = 0; b < wrongAnswers.size(); b++){
  Integer wrapper = (Integer)wrongAnswers.get(b);
  wrongAnswersArray[wrapper.intValue()-1]++;  
  }
 
  myFile.readLine();
  //myFile.readLine();
  for(int c = 0; c < 10; c++){
 
  String nextLine = myFile.readLine();
  holder[c] = nextLine;
  //System.out.println("test");
 
  }
 
  myFile.close();
  FileOutput myFileOut = new FileOutput("testResults.txt");
 
 
  myFileOut.println("questionsandanswers.txt");
  for(int d = 0; d < wrongAnswersArray.length; d++){
  myFileOut.println(d+1 + ":" + wrongAnswersArray[d]);
  }
 
  myFileOut.println("answersandquestions.txt");
  myFileOut.println("");
 
  for(int e = 0; e < 10; e++){
  myFileOut.println(holder[e]);
  }
 
   
  break;
 


 
case 2:
  myFile.readLine();
  for(int c = 0; c < 10; c++){
  String nextLine = myFile.readLine();
  holder[c] = nextLine;
 
  }
 
  myFile.readLine();
  myFile.readLine();
 
  for(int a = 0; a < wrongAnswersArray.length; a++){
  String nextLine = myFile.readLine();
  int dotLocation = nextLine.indexOf(":");
  int value = Integer.parseInt(nextLine.substring(dotLocation+1));
  wrongAnswersArray[a] = value;
 
  }
 
  for(int b = 0; b < wrongAnswers.size(); b++){
  Integer wrapper = (Integer)wrongAnswers.get(b);
  wrongAnswersArray[wrapper.intValue()-1]++;  
  }
  break;

default: break;

}

}

}


class Questions {
Random num = new Random();
private String question, answer, A,B,C,D,E;
ArrayList options = new ArrayList();
private int questId;
public Questions(int id, String quest, String An, String a, String b, String c, String d, String e){
question = quest;
questId = id;
answer = An;
options.add(a);
options.add(b);
options.add(c);
options.add(d);
options.add(e);
options.add(An);
}

public String getQuestion(){
return question;
}

public int getId(){
return questId;
}


public char getAnswer(){
return (char)answer.charAt(0);
}

public String mixThemUp(){
String quesAns= "";
int startLetter = 65;
int nextAns;
//System.out.println(options.size());
while (options.size() > 1){
//nextAns = num.nextInt(options.size()-1);
double nextAn = (Math.random() * 10);

while((int)nextAn >= options.size()-1){
nextAn = (Math.random() * 10);
}
nextAns = (int)nextAn;


//System.out.println(nextAns);
String stringOption = (String)options.get(nextAns);
if(stringOption.equals(answer))
{
answer = (char)startLetter + " ";
//System.out.println(answer);
}

quesAns+= (char)startLetter + "." + stringOption + "\n";
options.remove(nextAns);
startLetter++;
//nextAns = num.nextInt(options.size()-1);

}
return quesAns;
}

}



-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler

1:50 pm on Mar. 11, 2005 | Joined: Jan. 2003 | Days Active: 1,260
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | Posts: 6,458 | Points: 27,128
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
The text file I'm trying to write out too:

wera
1:0
2:0
3:0
4:0
5:0
6:0
7:0
8:0
9:0
10:0
were
1:0
2:0
3:0
4:0
5:0
6:0
7:0
8:0
9:0
10:0



-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler

1:51 pm on Mar. 11, 2005 | Joined: Jan. 2003 | Days Active: 1,260
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | Posts: 6,458 | Points: 27,128
firemouth


Dairy Product Addict

Patron
Reply
try this.

make sure the file you're trying to output into is a different name than the file you're trying to read from.

see if it works.

if it does, then when you're finished writing the file, make a quick function to either rename that file, or to delete the file you just inputted from, create a new file with the same name as the one you inputted from and copy all the data into the new one and then delete the output file.

-------
Science: "Why does this work?"
Engineering: "How does this work?"
Management: "When will this work?"
Liberal Arts: "Would you like fries with that?"


1:57 pm on Mar. 11, 2005 | Joined: July 2003 | Days Active: 215
Join to learn more about firemouth Michigan, United States | Straight Male | Posts: 1,095 | Points: 5,319
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
Your a genious. I created a new file and renamed it when I was done. Thank you!

-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler

2:06 pm on Mar. 11, 2005 | Joined: Jan. 2003 | Days Active: 1,260
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | Posts: 6,458 | Points: 27,128
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic