Hello Java developers,
I am not so old in java and still on my learning period.
I was practicing java and came up with a problem.
The description is below.
Oliver and Lucy play game "Guess: which letter will be the last one?". Lucy names a guessed word and asks Oliver, which letter will be the last one. Rules of the games are the following:
(a) at first, remove every second letter,
(b) then reverse the string of remaining letters (turn in backward order),
(c) repeat steps a and b until single letter left.
For example, when Lucy names word "KARTUPELIS", the wright answer is "I", because KARTUPELIS -> KRUEI -> IEURK -> IUK -> KUI -> KU -> IK -> I.
Write program that reads data from text file uzd9.in the given words; each word is written in separate line; maximum length of the word is 30 letters; word can include both uppercase, and lowercase Latin alphabet letters; the file contains no more than 100 words. The program must write the last remaind letter for each word after the gameplay in text file uzd9.out; each result in separate line.
Example, Input data Wordsbefore.in KARTUPELIS BUMBA KURMIS SUNS
Output data wordsafter.out I A K N
.
below is what i have tried. It kinda works, but, i have to convert the word to chararray everytime and then run a loop through it. It gives result but, to me it is not a good practice. I want to run a loop through those words and keep running until single letter is left in all words.
Please help me that, what can i do to run a cycle that, goes through the words, does it's job and checks if single letter is left. if not, then does the same thing and keeps doing until the single letter is left in each word.
"After running the code below, the output that i get is:"
"
kui"
I have "
kartupeli" in my txt file.
What I have tried:
package com.io.java;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class buffered {
public static void main(String[] args)
{
buffered b = new buffered();
String textFile = "words.txt";
b.readFile(textFile);
String newstr = "";
}
public void readFile(String strFile)
{
try (BufferedReader buffer = new BufferedReader(new FileReader(strFile)))
{
String strBuffer;
while((strBuffer = buffer.readLine()) != null && strBuffer.length()>1)
{
String n = "";
String newtr = "";
int k;
char[] c = strBuffer.toCharArray();
for( k=0; k<=c.length;k++)
{
if(k%2==0)
{
newtr+=c[k];
}
}
newtr = reverse(newtr);
c=newtr.toCharArray();
if(c.length>0)
{
for(int j = 0; j<=c.length;j++)
{
if(j%2==0)
{
n+=c[j];
}
}
}
else
{
break;
}
n = reverse(n);
System.out.println(n);
}
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeFile(String strFile, String data)
{
try(BufferedWriter bwriter = new BufferedWriter(new FileWriter(strFile, true)))
{
bwriter.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
public String reverse(String s)
{
s = new StringBuffer(s).reverse().toString();
return s;
}
}