/*
* This program reads a message from a file and follows the text messaging rule
* There should only be a maximum of 3 sets
* Each set should only have a maximum of 160 characters
* If it happens that a certain letter in a word is the 160th, that word will not be cut
* but be moved to the next set
* Output will be written on the console*/
import java.io.*;
import java.util.StringTokenizer;
public class SMS {
public static void main(String args[]) {
int three=1; //stores the number of sets the message will occupy
int character=0; //will be used to indicate if 160 characters per set is exceeded
int wordlen=0;
String temp="";
int message=1;
PrintWriter outputStream=null;
try{
outputStream= new PrintWriter(new FileOutputStream("out.txt")); //to output contents on a file
System.out.println("HI !The contents of your text message are now written in out.txt.\n");
three=checkLength(); //checks how many sets does this message will occupy
BufferedReader inputStream= new BufferedReader(new FileReader("text.txt")); //reads input from file
String line=null;
line=inputStream.readLine(); //read line from file
outputStream.print("\n1/"+three+" "); //display the corresponding set number
while(line!=null){ //while line read is not null
StringTokenizer tokens= new StringTokenizer(line," \n"); //create a stringtokenizer, this eliminates spaces and newlines
while(tokens.hasMoreTokens()){ //while there are still tokens left
temp=tokens.nextToken(); //get next token
wordlen=temp.length(); //get length of the token
character=character+wordlen; //add length to character
if(character>=160){ //if characters read now exceed 160
character=wordlen;
message++;
if(message<=3)
outputStream.print("\n"+message+"/"+three+" "); //indicates another set
}//end if
if(message<=3){
outputStream.print(temp+" "); //ensure that the text message will occupy 3 sets, a space is concatenated after each word
character++;
}//end if
}//end while
line=inputStream.readLine(); //read next line from the file
}//end while
outputStream.println();
outputStream.close();
inputStream.close();
}//end try
catch (FileNotFoundException e) {
System.out.println("The file text.txt could not be found");
System.out.println("or could not be opened.");
}
catch (IOException e) {
System.out.println("Error reading from text.txt");
}
}//end main
public static int checkLength(){ //checks the number of sets that the message will occupy
int many=1;
int character=0;
String temp=null;
String linefromstream=null;
try{
BufferedReader inputStream= new BufferedReader(new FileReader("text.txt"));
String line=null;
int linelen=0;
linefromstream=inputStream.readLine();
while(linefromstream!=null){
StringTokenizer tokens= new StringTokenizer(linefromstream," \n"); //create a stringtokenizer, this eliminates spaces and newlines
while(tokens.hasMoreTokens()){
temp=tokens.nextToken();
line=line+temp+" "; //the purpose of this is to count double or multiple spaces as one space and to treat newlines as a single space
}//end while
if(line!=null)
linelen=line.length();
character=character+linelen; //calculate total characters in the message
if(character<=160)
many=1;
if(character>160 && character<=320)
many=2;
if(character>320)
many=3;
linefromstream=inputStream.readLine(); //read next line
line=null; //reset variables
linelen=0;
}//end while
}//end try
catch (FileNotFoundException e) {
System.out.println("Error reading from text.txt! Could not perform instructed method.");
}//end try
catch (IOException e) {
System.out.println("Error reading from text.txt");
}
return many;
}//end check
}//end class
SMS.rar_The Rules
版权申诉
150 浏览量
2022-09-20
16:18:47
上传
评论
收藏 1KB RAR 举报
APei
- 粉丝: 64
- 资源: 1万+