-1

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

12
  • 1
    Show us what you have now and let's work from there ;) Commented Dec 14, 2012 at 12:12
  • 5
    Must use a different google - "java filling an array of characters from user input" first hit is stackoverflow.com/questions/2622725/… Commented Dec 14, 2012 at 12:13
  • How about we try breaking this into smaller chunks? Start with a search for "java keyboard input" and work from there. Commented Dec 14, 2012 at 12:14
  • I know how to fill an array of integers, etc. the problem is with characters. Commented Dec 14, 2012 at 12:16
  • 1
    @Aura It won't go around more than 10 times, which is less than infinite. Commented Dec 14, 2012 at 12:49

9 Answers 9

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

Sign up to request clarification or add additional context in comments.

11 Comments

The problem is with characters, not integers.
@Aura, then you should mention this, in your question, be clear with your question.
i have mentioned, both in title and the question.
@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.
i'm a beginner. Did u know anything about IOexception or BufferedReader when u start java programming?
|
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]);
        }
    }
}

1 Comment

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]);
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();

Comments

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]);
                }
        }

Comments

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

}

Comments

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();
    }   

   }
  }

1 Comment

That's rather a large chunk of code without any explanation of what you're doing or where the results end up.
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
    }
}

Comments

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);
        }
    }*

Comments

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]);
    }
    }
    }

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.