I googled it a lot and found nothing! Could someone help me with filling an array of characters from user input, please?
-
1Show us what you have now and let's work from there ;)Averroes– Averroes2012-12-14 12:12:51 +00:00Commented Dec 14, 2012 at 12:12
-
5Must use a different google - "java filling an array of characters from user input" first hit is stackoverflow.com/questions/2622725/…Andreas Fester– Andreas Fester2012-12-14 12:13:53 +00:00Commented 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.MildWolfie– MildWolfie2012-12-14 12:14:43 +00:00Commented Dec 14, 2012 at 12:14
-
I know how to fill an array of integers, etc. the problem is with characters.Aura– Aura2012-12-14 12:16:35 +00:00Commented Dec 14, 2012 at 12:16
-
1@Aura It won't go around more than 10 times, which is less than infinite.Peter Lawrey– Peter Lawrey2012-12-14 12:49:24 +00:00Commented Dec 14, 2012 at 12:49
9 Answers
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 :
11 Comments
//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
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]); /* 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
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
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
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
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
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]);
}
}
}