0

I have this code in Java,

package bookpurchased;

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Book {

public static void getInputFromScanner() {
    Scanner input = new Scanner(System.in);  
    System.out.print("Enter the Number you want: ");  
    Short bookId = input.nextShort(); 

    System.out.println("\nBook Price : ");
    double bookPrice = input.nextDouble();
    
    System.out.println("The Book Category is: " + bookId);
    System.out.println("The Book Price is: " + bookPrice);
    input.close();  
}

public static void getInputFromReader() {
    BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
    
    String bookTitle = "";
    System.out.print("Enter book title");
    

here below it: I want to change the bookCategory from String to char, but if I change, I have received a lots of error. It's like a string when in run but I have to change only a bookCategory from String to char.

    String bookCategory = "";
    System.out.print("Enter book title");
    
    try {
        bookTitle = dataIn.readLine();
        bookCategory = dataIn.readLine();
    }catch(IOException e) {
        System.out.println("Error!");
    }
    System.out.println("The book title is: " + bookTitle);
    System.out.println("The book category is: " + bookCategory);
    
}
public static void main(String[]args) {
getInputFromReader();
getInputFromScanner();
}
}
Jah
  • 1
  • 3
  • You can reference this: https://stackoverflow.com/questions/7853502/how-to-convert-parse-from-string-to-char-in-java – bartholomew Mar 03 '22 at 18:30

1 Answers1

0

You can make of String.toCharArray() i.e., bookCategory.toCharArray() or dataIn.readLine().toCharArray() will give you char[] array.

But you have to intialize the bookCategory to char array or you can print directly the converted char[] array using Arrays.toString()