this is my portion of the code where I would like to store into a file. I am jus
ID: 3539065 • Letter: T
Question
this is my portion of the code where I would like to store into a file. I am just uncerrtain on how to
get the characters from the method to store either into an array or a new string so that I can save it to a file.
private static void decodeMessage(String phrase)throws IOException {
String temp;
int j=0;
phrase = phrase.toUpperCase();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isLetter(phrase.charAt(i))) {
for ( j = 0; j < alpha.length; j++) {
if (phrase.charAt(i) == alpha[j]) {
if (j == 0) {
System.out.print(alpha[alpha.length - 1]);
} else {
System.out.print(alpha[j - 1]);
// int []decoded=new int[alpha[j-1]];
temp=Character.toString(alpha[j-1]);
}
}
}
} else {
System.out.print(phrase.charAt(i));
}
Scanner decinput=new Scanner(new File("C: ew.txt"));//I want to save my new string/character array here.
// BufferedWriter bw = new BufferedWriter(decinput);
// PrintWriter pw = new PrintWriter(decinput);
// pw.close();
}
Explanation / Answer
change the return type to string. then return the required string to the main function where you will save it to a file
public class solution
{
public String decodeMessage(String phrase)throws IOException {
String temp;
int j=0;
phrase = phrase.toUpperCase();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isLetter(phrase.charAt(i))) {
for ( j = 0; j < alpha.length; j++) {
if (phrase.charAt(i) == alpha[j]) {
if (j == 0) {
System.out.print(alpha[alpha.length - 1]);
} else {
System.out.print(alpha[j - 1]);
// int []decoded=new int[alpha[j-1]];
temp=Character.toString(alpha[j-1]);
}
}
}
} else {
System.out.print(phrase.charAt(i));
}
return temp;
}
public static void main()
{
String phrase;
// input phrase here
String output = decodeMessage(phrase);
try
{
BufferedWriter bw = new BufferedWriter(decinput);
bw.write(output);
bw.close();
}
catch(IOException e) {
e.printStackTrace(); }
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.