LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 746 users online 224652 members 1440 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
Googoie
Movies: I lost The Game.
Mood: Bored
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
7 online / 25 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Adding Reply

Quoting Post
Archived Topic: It will not be bumped to the top of the forum.
Topic Java FileI/O
Membername   Not a member? Sign Up Free (takes 20 seconds)
Password   Forgotten your password?
Post

Font:   Size:   Color:

FAQ Keyword Search:
Post Options
Favorites Manager
Notify me of new replies to this topic by email
Notify me of new replies to this topic by private message
Original Post
squirellplayingtag Posted at 1:40 pm on Mar. 11, 2005
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]);
  }

Replies
squirellplayingtag Posted at 2:06 pm on Mar. 11, 2005
Your a genious. I created a new file and renamed it when I was done. Thank you!
firemouth Posted at 1:57 pm on Mar. 11, 2005
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.

squirellplayingtag Posted at 1:51 pm on Mar. 11, 2005
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

squirellplayingtag Posted at 1:50 pm on Mar. 11, 2005
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;
}

}

firemouth Posted at 1:44 pm on Mar. 11, 2005
can we see the whole program?

this isnt enough

All 5 previous replies displayed.