-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
  • 1
    Show us what you have now and let's work from there ;) – Averroes Dec 14 '12 at 12:12
  • 5
    Must use a different google - "java filling an array of characters from user input" first hit is http://stackoverflow.com/questions/2622725/how-to-take-user-input-in-array-using-java – Andreas Fester Dec 14 '12 at 12:13
  • How about we try breaking this into smaller chunks? Start with a search for "java keyboard input" and work from there. – MildWolfie Dec 14 '12 at 12:14
  • I know how to fill an array of integers, etc. the problem is with characters. – Aura Dec 14 '12 at 12:16
  • @Aura If you know how to do it with integers, use that as a starting point. Try to convert it to use characters, and then post the code here when you are stuck. – Andreas Fester Dec 14 '12 at 12:17
  • Post your basic code here and we will try to help you out . – The Dark Knight Dec 14 '12 at 12:19
  • I tried these foolish codes: `char[] charArr = new char[10]; for (int i = 0; i < charArr.length; i++) { charArr[i] = (sc.next()).charAt(i); }` – Aura Dec 14 '12 at 12:21
  • @Aura I would add that to your question. It's a good start, what problem did you have with it? It will take the first letter of 10 words. – Peter Lawrey Dec 14 '12 at 12:22
  • @PeterLawrey It makes an infinit loop. – Aura Dec 14 '12 at 12:30
  • @ivanovic think that I don't even know how to google a question. Would you please show me one of your search results? here's a place to share our knowledge, isn't it? – Aura Dec 14 '12 at 12:34
  • 1
    @Aura It won't go around more than 10 times, which is less than infinite. – Peter Lawrey Dec 14 '12 at 12:49
  • @PeterLawrey :-? now it works and i don't know how! tnx anyway... – Aura Dec 14 '12 at 12:57

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
  • 30,829
  • 42
  • 119
  • 173
  • 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
  • 319
  • 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
  • 525,659
  • 79
  • 751
  • 1,130
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
  • 109
  • 1
  • 6
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
  • 173
  • 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,409
  • 6
  • 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