Program to print a random quote from a set of quotes stored in a file "Quotes.txt" such that the quotes will not be repeated unless all the quotes are exhausted.
import java.util.*;
import java.io.*;
public class MathsQuoteOfDay
{
public static void main(String[] args) throws IOException
{
int n=0,input,j=0,nq=0,k;
boolean flag=true;
FileReader fr = new FileReader("Quotes.txt");
BufferedReader br = new BufferedReader(fr);
String S;
while(( S=br.readLine() ) != null)
n++;
fr.close();
Quotes Q = new Quotes(n);
int[] qIndex = new int[60];
FileReader fr1 = new FileReader("Quotes.txt");
BufferedReader br1 = new BufferedReader(fr1);
while(( S=br1.readLine() ) != null)
Q.setQuotes(S);
fr1.close();
/*FileOutputStream fout1 = new FileOutputStream("Data.txt");
DataOutputStream out1 = new DataOutputStream(fout1);
out1.close();*/
FileInputStream fi = new FileInputStream("Data.txt");
DataInputStream i = new DataInputStream(fi);
int l;
boolean EOF =false;
while(!EOF)
{
try {
qIndex[j] =i.readInt();
//System.out.println(qIndex[j]);
j++;
}catch (EOFException e) {
EOF=true;
}
}
i.close();
//j=j-1;
//System.out.println("J=>>>>>>>>>"+j);
//for(int t=0; t<j;t++)
//System.out.println("Array"+qIndex[t]);
if(j!=0 && j!=n)
{
boolean match=true;
boolean find=false;
int t;
do
{
find=false;
t = (int)(Math.random()*n);
//System.out.println("Random value is"+t);
for(int m=0;m<j;m++)
{
if(t==qIndex[m])
find=true;
}
if(find==true)
match=false;
else
match=true;
}while(match==false);
FileOutputStream fout = new FileOutputStream("Data.txt",true);
DataOutputStream out = new DataOutputStream(fout);
out.writeInt(t);
//System.out.println("Ought to be 9"+t);
System.out.println("");
System.out.println(" "+Q.getQuotes(t));
System.out.println("");
out.close();
}
else
{
int t;
//System.out.println("Exhausted");
t = (int)(Math.random()*n);
FileOutputStream fo = new FileOutputStream("Data.txt");
DataOutputStream o = new DataOutputStream(fo);
o.writeInt(t);
System.out.println("");
System.out.println(" "+Q.getQuotes(t));
System.out.println("");
o.close();
}
}
}
class Quotes
{
Quotes()
{
}
Quotes(int n)
{
quotes = new String[n];
q=0;
}
public void setQuotes(String S)
{
quotes[q++]=S;
}
public String getQuotes(int ran)
{
return (quotes[ran]);
}
private String[] quotes;
private int q;
}
import java.util.*;
import java.io.*;
public class MathsQuoteOfDay
{
public static void main(String[] args) throws IOException
{
int n=0,input,j=0,nq=0,k;
boolean flag=true;
FileReader fr = new FileReader("Quotes.txt");
BufferedReader br = new BufferedReader(fr);
String S;
while(( S=br.readLine() ) != null)
n++;
fr.close();
Quotes Q = new Quotes(n);
int[] qIndex = new int[60];
FileReader fr1 = new FileReader("Quotes.txt");
BufferedReader br1 = new BufferedReader(fr1);
while(( S=br1.readLine() ) != null)
Q.setQuotes(S);
fr1.close();
/*FileOutputStream fout1 = new FileOutputStream("Data.txt");
DataOutputStream out1 = new DataOutputStream(fout1);
out1.close();*/
FileInputStream fi = new FileInputStream("Data.txt");
DataInputStream i = new DataInputStream(fi);
int l;
boolean EOF =false;
while(!EOF)
{
try {
qIndex[j] =i.readInt();
//System.out.println(qIndex[j]);
j++;
}catch (EOFException e) {
EOF=true;
}
}
i.close();
//j=j-1;
//System.out.println("J=>>>>>>>>>"+j);
//for(int t=0; t<j;t++)
//System.out.println("Array"+qIndex[t]);
if(j!=0 && j!=n)
{
boolean match=true;
boolean find=false;
int t;
do
{
find=false;
t = (int)(Math.random()*n);
//System.out.println("Random value is"+t);
for(int m=0;m<j;m++)
{
if(t==qIndex[m])
find=true;
}
if(find==true)
match=false;
else
match=true;
}while(match==false);
FileOutputStream fout = new FileOutputStream("Data.txt",true);
DataOutputStream out = new DataOutputStream(fout);
out.writeInt(t);
//System.out.println("Ought to be 9"+t);
System.out.println("");
System.out.println(" "+Q.getQuotes(t));
System.out.println("");
out.close();
}
else
{
int t;
//System.out.println("Exhausted");
t = (int)(Math.random()*n);
FileOutputStream fo = new FileOutputStream("Data.txt");
DataOutputStream o = new DataOutputStream(fo);
o.writeInt(t);
System.out.println("");
System.out.println(" "+Q.getQuotes(t));
System.out.println("");
o.close();
}
}
}
class Quotes
{
Quotes()
{
}
Quotes(int n)
{
quotes = new String[n];
q=0;
}
public void setQuotes(String S)
{
quotes[q++]=S;
}
public String getQuotes(int ran)
{
return (quotes[ran]);
}
private String[] quotes;
private int q;
}
No comments:
Post a Comment