-1

I googled it a lot and found nothing! Could someone help me with filling an array of characters from user input, please?

Aura
  • 61
  • 1
  • 1
  • 5

9 Answers9

7

I googled it a lot and found nothing! Could someone help me with filling an array of characters from user input, please?

My Google said, try this one..

Option 1 :

    import java.io.*;
   class array {

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String tmp = br.readLine();
        int length = tmp.length();
        char c[] = new char[length];
        tmp.getChars(0, length, c, 0);
        CharArrayReader input1 = new CharArrayReader(c);
        int i;
        System.out.print("input1 is:");
        while ((i = input1.read()) != -1) {
            System.out.print((char) i);
        }

    }
}

Option 2:

class array
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter elements...");
        char[] a=sc.next().toCharArray();
        System.out.println("Array elements are : ");
        for (int i=0;i<a.length;i++)
            System.out.println(a[i]);
    }
}

But, in this case, it won't accept after space character.

Before, start your coding in Java, you must know these terms :

BufferedReader

Exception handling

Ravi
  • 29,945
  • 41
  • 114
  • 168
  • The problem is with characters, not integers. – Aura Dec 14 '12 at 12:23
  • @Aura, then you should mention this, in your question, be clear with your question. – Ravi Dec 14 '12 at 12:24
  • i have mentioned, both in title and the question. – Aura Dec 14 '12 at 12:27
  • @Aura check my updated post. It shows, you didn't tried a single piece of code. These are most basic code, you can find them very easily. – Ravi Dec 14 '12 at 12:41
  • 1
    i'm a beginner. Did u know anything about IOexception or BufferedReader when u start java programming? – Aura Dec 14 '12 at 12:46
  • did you know anything about exception handling ?? – Ravi Dec 14 '12 at 12:48
  • i have java course at university and i'm new to it. of course i don't! – Aura Dec 14 '12 at 12:51
  • Actually it's my teacher's fault... tnx for ur time and help; i learned a lot. – Aura Dec 14 '12 at 13:04
  • @Aura don't blame your teacher, better to take initiative yourself. – Ravi Dec 14 '12 at 13:10
  • @coders, man!! you were desperate to get the answer accepted? :P – LPD Dec 14 '12 at 13:51
  • @LPD hahaha.... no it wasn't like that, actually the way he/she asked the question, `i googled,but didn't find anything`, that's why, i was just showing him/her. I found all this from google. ;) – Ravi Dec 14 '12 at 15:04
1

//more fun ...............

public class test3 {

    public static void main(String args[])
    {
        char crr[]=new char[100];
        Scanner inputs=new Scanner(System.in);
        System.out.println("enter the string");
        for(int i=0;i<10;i++)
        {
            char c=inputs.next().charAt(0);
            crr[i]= c;
        }
        for(int i=0;i<10;i++)
        {
            System.out.println(" " +crr[i]);
        }
    }
}
Sorack
  • 289
  • 1
  • 5
  • 18
MridulB
  • 11
  • 1
  • This is exactly what I was looking for! Have been looking for a way to sort an array full of characters that were inputted by the user. `public static void main(String[] args) { char crr[]=new char[10]; Scanner inputs=new Scanner(System.in); System.out.println("Enter 10 Letters: "); for(int i=0;i<10;i++) { char c=inputs.next().charAt(0); crr[i]= c; } System.out.println("The Sorted Letters are: "); for(int i=0;i<10;i++) { Arrays.sort(crr); System.out.println(" " +crr[i]);` – Brent Knox Jan 26 '17 at 16:37
0

If you want to be able to read a word and split it into an array of characters you can use.

char[] chars = scanner.next().toCharArray();
Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106
0
   /* program below takes an string from user, splits into character and display as array of characters. */  

     package com.demo.mum;

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

        /**
         * @author cyruses
         * 
         */

        public class CharacterArray {

            public static void main(String args[]) throws IOException {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter the string:");
                String tmp = br.readLine();
                int strLen = tmp.length();
                char c[] = new char[strLen];
                for (int i = 0; i < c.length; i++) {
                    c[i] = tmp.charAt(i);
                }
                System.out.println("Displaying character Array");
                for (int i = 0; i < c.length; i++) {
                    System.out.println(c[i]);
                }
        }
Cyruses Cyrus
  • 59
  • 1
  • 5
0

This code is part of my program for linear searching, I fixed the issue I was facing. But I want explanation about why I was getting exception on charAt(x) and not on charAt(0).

        System.out.println("Enter Your Data in character");
        for(x=0;x<char_array.length;x++)
        {
        Scanner input_list_char = new Scanner(System.in);
        char_array[x]=input_list_char.next().charAt(0); //it works
        char_array[x]=input_list_char.next().charAt(x); // give me exception

}

Maverick
  • 163
  • 4
  • 10
0

To input a character array from user

import java.io.*;
class CharArrayInput {

public static void main(String args[]) throws IOException {

    /*using InputReader and BufferedReader class 
      to fill array of characters from user input.
    */
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    //Take size of array from user.
    System.out.println("Please enter size of array")
    int n = Integer.parseInt(br.readLine());
    //Declare a character array
    char arr[] = new char[n];
    //loop to take input of array elements
    for(int i=0; i < n; i++){
    arr[i] = (char)br.read();
    }   

   }
  }
0

You can't take input directly in charArray using nextChar() because there is no nextChar() in Java. You first have to take input in String then fetch character one by one.

import java.util.*;
class CharArray{
    public static void main(String[] args)
    { 
    Scanner scan=new Scanner(System.in); 

    char ch[]=new char[11];

    String s = scan.nextLine();

    for(int i=0;i<=10;i++)  
    ch[i]=s.charAt(i);  //Input in CharArray

    System.out.println("Output of CharArray: ");
        for(int i=0;i<=10;i++) 
        System.out.print(ch[i]); //Output of CharArray
    }
}
0

this will not work if user print input in form of string like as...

5 8
########
#..#...#
####.#.#
#..#...#
########

in this if we use char c=inputs.next().charAt(0); it will take only first of every string ,

It will be better to use

    *Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    int m = s.nextInt();
    char[][] in = new char[m+1][n+1];
    for(int i = 0;i<n;i++) {
        String st = s.next();
        for(int j = 0;j<m;j++) {
            in[i][j] = st.charAt(j);
        }
    }*
Detonar
  • 1,399
  • 4
  • 18
0
    class charinarray
    {
    public static void main(String args[])
    {
    Scanner input=new Scanner(System.in);
    System.out.println("Please enter char elements...");
    char[] a=input.next().toCharArray();
    System.out.println("Array char elements are : ");
    for (int i=0;i<a.length;i++)
    {
        System.out.println(a[i]);
    }
    }
    }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 05 '21 at 07:46