❓ Help Programming Java language

Status
Not open for further replies.

mochito

Enthusiast
In the main, continuously ask the user to input a character (one per line) and check if it is letter or a number. If the character is neither a letter or a number, stop asking for inputs.First, create a text file named inputted, txt for the folder namedletters. Write the characters which are letters into this file. Then, create a text file with the same name but inside the folder name numbers. Finally, write the characters which are numbers into the latter text file.
Sample Output 1
Enter a character: a
Enter a character: b
Enter a character: c
Enter a character: 1
Enter a character: 2
Enter a character: 3
Enter a character: .
 
To implement the desired functionality in Java, you can use the following code. This program continuously asks the user to input a character and checks if it is a letter or a number. It then writes the characters to different files based on whether they are letters or numbers.

Java:
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class CharacterInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        try {
            FileWriter lettersFile = new FileWriter("letters/inputted.txt");
            FileWriter numbersFile = new FileWriter("numbers/inputted.txt");
            
            while (true) {
                System.out.print("Enter a character: ");
                String input = scanner.nextLine();
                
                if (input.length() != 1) {
                    break;
                }
                
                char c = input.charAt(0);
                
                if (Character.isLetter(c)) {
                    lettersFile.write(c);
                } else if (Character.isDigit(c)) {
                    numbersFile.write(c);
                } else {
                    break;
                }
            }
            
            lettersFile.close();
            numbersFile.close();
            
            System.out.println("Files created successfully.");
        } catch (IOException e) {
            System.out.println("An error occurred while writing to the files.");
            e.printStackTrace();
        } finally {
            scanner.close();
        }
    }
}

In this program:
1. We use FileWriter to write characters to files.
2. We continuously ask the user for input using a while loop.
3. We check if the input character is a letter or a number using Character.isLetter() and Character.isDigit() methods.
4. Based on the check, we write the character to the respective file.
5. If the input is not a letter or a number, the program stops asking for input and closes the files.
6. Finally, we handle exceptions that might occur during file writing and close the scanner.

Make sure you have the 'letters' and 'numbers' folders created in the same directory as the Java file before running the program.
 
Can you separate the try and catch of letters and numbers of this code?

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Main{
public static void main(String [] args){
File numbersDir = new File ("numbers");
File lettersDir = new File ("letters");
if(!numbersDir.exists()){
numbersDir.mkdirs();
}
if(!lettersDir.exists()){
lettersDir.mkdirs();
}

File numbersFile = new File(numbersDir, "inputted.txt");
File lettersFile = new File(lettersDir, "inputted.txt");

try(
FileWriter numbersWriter = new FileWriter(numbersFile, true);
FileWriter lettersWriter = new FileWriter(lettersFile, true);
Scanner scanner = new Scanner(System.in);
){
while(true) {
System.out.print("Enter a character: ");
String input = scanner.nextLine();
if(input.length()!= 1){
continue;
}
char ch = input.charAt(0);

if(Character.isDigit(ch)){
numbersWriter.write(ch + "\n");
} else if (Character.isLetter(ch)){
lettersWriter.write(ch + "\n");
}else {
break;
}
}
} catch(IOException e){
e.printStackTrace();
}
}
}
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 376
    Views
  • 1
    Participants
Last reply from:
mochito

Trending Topics

Online now

Members online
1,217
Guests online
1,641
Total visitors
2,858

Forum statistics

Threads
2,273,490
Posts
28,949,866
Members
1,235,767
Latest member
androiddigial
Back
Top