0
// I have to prompt the user to input the String and store into Char [] array
// I know that we can use .toCharArray instance method to store the string
// into Character array.

// But I do not have to use that method, so I reference the string input to
// char array but I got an compiler error saying cannot convert string to char

// This is I have done So far
import java.util.Scanner;

/** This program prompts the user to input a string and then outputs the entire string uppercase letters. */

public class StringUppercase
{
    public static void main(String [] args)
    {
        String input;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a String " );
        input = keyboard.nextLine();

        char[] name;

    }
} 
Smit Patel
  • 7
  • 2
  • 6
  • 3
    It's silly to not use that method. I suppose you could loop over the String and call `charAt(i)` for each character. – Thilo Apr 23 '15 at 03:02
  • @Thilo It is silly, but It's an lab assignment so have to flow according to it. – Smit Patel Apr 23 '15 at 03:04
  • Related: http://stackoverflow.com/questions/25011735/reading-a-string-in-character-array-without-using-any-string-function-not-even?rq=1 – Thilo Apr 23 '15 at 03:19
  • Slightly less silly (but quite difficult, especially if you need to support non-ASCII) would be to try to skip String entirely and read characters directly off System.in. http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it?lq=1 – Thilo Apr 23 '15 at 03:20

2 Answers2

0

Here is code snippet to convert String in to char[] array

    String input;
    Scanner keyboard = new Scanner(System.in);
    input = keybaord.nextLine();
    char[] nameChar = new char[input.length()];
    for(int i =0; i < name.length() ; i++)
    {
        nameChar[i] = name.charAt(i);
    }
    System.out.println(nameChar);
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
0

Here is code snippet to convert String into an char Array and output it in UPPERCASE.

public static void main(String[] args)
    {
        String inputString;
        System.out.println("Please Enter the String");
        Scanner sc = new Scanner(System.in);
        inputString = sc.nextLine();
        sc.close();
        int len=inputString.length();
        char[] name = new char[len];
        for(int i =0; i < len; i++)
        {
            name[i] = inputString.toUpperCase().charAt(i);
        }
        System.out.println(name);
    }
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44