'Making vowels in a charArray lowercase
First time posting here, so I have this assignment where we make a code that reads a file and writes it into another one, except in uppercase, however we also need to add a statement to exempt vowels from being uppercase.
Main:
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
// write your code here
String iFilePath, oFilePath;
iFilePath = "C:\\Users\\flame\\Desktop\\New folder\\FileforRead.txt";
oFilePath = "C:\\Users\\flame\\Desktop\\New folder\\FileforWrite.txt";
UpperCaseFile ucf = new UpperCaseFile(iFilePath, oFilePath);
}
}
UpperCaseFile class:
package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.lang.String;
import static java.lang.Character.toLowerCase;
public class UpperCaseFile {
public UpperCaseFile(String iFile, String oFile) throws FileNotFoundException {
String input;
String output;
File file;
file = new File(iFile);
Scanner inputFile = new Scanner(file);
PrintWriter outputFile = new PrintWriter(oFile);
while (inputFile.hasNext()) {
input = inputFile.nextLine();
output = input.toUpperCase();
outputFile.println(output);
char[] charArray = iFile.toCharArray();
for (int i = 0; i < charArray.length; i++)
{
if ( charArray[i] == 'a' || charArray[i] == 'e' ||
charArray[i] == 'i' || charArray[i] == 'o' ||
charArray[i] == 'u')
{
charArray[i] = Character.toLowerCase(charArray[i]);
}
inputFile.close();
outputFile.close();
}
}
}
}
In all honesty, I can't seem to find the error, at least my IDE isn't saying so and it always ends with finishing the process with exit code 1. All help would be much appreciated, thank you.
Solution 1:[1]
I have added comments, what you need to do, and what wrong you are doing here. try to write code what I have said in comment. If you face any issue do comment here will help along way. I am not providing code for problem since it is assignment.
while (inputFile.hasNext()) {
input = inputFile.nextLine();
output = input.toUpperCase();
// here you will do, what you want to do.
// you will take output string and find vowel and convert those
// into lowercase. Keep in mind that all characters along with
// vowels are already capitals, so find all capital vowels and convert
// those to lowercase
// after above operation you can write into outputfile
outputFile.println(output);
// this is not doing what you are doing
/* char[] charArray = iFile.toCharArray();
for (int i = 0; i < charArray.length; i++)
{
if ( charArray[i] == 'a' || charArray[i] == 'e' ||
charArray[i] == 'i' || charArray[i] == 'o' ||
charArray[i] == 'u')
{
charArray[i] = Character.toLowerCase(charArray[i]);
}*/
//You need to close file after doing operations.
// these lines should not be in while
//inputFile.close();
// outputFile.close();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Awais Latif |
