character input in an array in java.....how?????
Page 1 of 2 • Share •
Page 1 of 2 • 1, 2 
character input in an array in java.....how?????
I have faced a problem in java program. That is how can i get input in an array in java. just like this:
in an array[3], a[0]='a', a[1]='b', and a[2]='c';
please help me by giving the solution.
in an array[3], a[0]='a', a[1]='b', and a[2]='c';
please help me by giving the solution.

BIT0216-Habib- Administrator-RC
- Course(s):
- BIT
Blood Group: O+
Posts: 217
Points: 458
Re: character input in an array in java.....how?????
Sorry didn't get you.
btw
you can use char array like
btw
you can use char array like
- Code:
char[] yourArray = new char[3];
yourArray[0] = 'a';
yourArray[1] = 'b';
yourArray[2] = 'c';
_________________________________________________________________
Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী

BIT0112-Rokon- Programmer

- Course(s):
- BIT
Blood Group: O+
Posts: 673
Points: 1269

Re: character input in an array in java.....how?????
btw why it is here.. there is a Java help section.. post Java problems there
_________________________________________________________________
Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী

BIT0112-Rokon- Programmer

- Course(s):
- BIT
Blood Group: O+
Posts: 673
Points: 1269

Re: character input in an array in java.....how?????
this topic has been moved to the java help section

BIT0103-Ovid- Release Candidate

- Course(s):
- BIT
Blood Group: O-
Posts: 150
Points: 261
Re: character input in an array in java.....how?????
BIT0103-Ovid wrote:this topic has been moved to the java help section
oops
I think you have enabled "leave a shadow topic" 
So, I can see this:

I am now going to delete the shadow topic, but if somehow deletes the topic under java section too, blame Alim
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

convert C program into java.........
can anyone help me by converting this C code into java code:
heres the code:
#include
#include
int main(void){
char array[3];
for(int i=0; i<3; i++){
scanf("%c",&array[i]);
}
for(int i=0; i<3; i++){
printf("%c",array[i]);
}
getch();
return 0;
}
heres the code:
#include
#include
int main(void){
char array[3];
for(int i=0; i<3; i++){
scanf("%c",&array[i]);
}
for(int i=0; i<3; i++){
printf("%c",array[i]);
}
getch();
return 0;
}

BIT0216-Habib- Administrator-RC
- Course(s):
- BIT
Blood Group: O+
Posts: 217
Points: 458
Re: character input in an array in java.....how?????
here you go:
read the comments for explanation.
Sample Input:
sample output
there is also several other methods for doing the same task in more efficient way(for example, bufferreader, or the System.in.read() method) But as you are only a beginner and possibly only know about Scanner till now, use this.
And you better use the code tag next time when you are posting a code.
AND
DO NOT USE ... IN TOPIC TITLE.
why?
1. It looks ugly.
2. It looks horrible.
3. It is what you use in chatting. Are you chatting?
- Code:
package myPackage;
import java.util.Scanner;
public class ForHabib {
public static void main(String[] args) {
//use Scanner to take input
Scanner myScanner = new Scanner(System.in);
//that's your character array
char[] myArray = new char[3];
for (int i = 0; i < myArray.length; i++) {
//as we don't have any nextChar() method, we are going to take input and keep it in a string
String temp = myScanner.next();
//and then we are going to put the first character of that string to your array
myArray[i] = temp.charAt(0);
}
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
}
}
read the comments for explanation.
Sample Input:
- Code:
ami
bas
qwe
sample output
- Code:
a
b
q
there is also several other methods for doing the same task in more efficient way(for example, bufferreader, or the System.in.read() method) But as you are only a beginner and possibly only know about Scanner till now, use this.
And you better use the code tag next time when you are posting a code.
AND
DO NOT USE ... IN TOPIC TITLE.
why?
1. It looks ugly.
2. It looks horrible.
3. It is what you use in chatting. Are you chatting?
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: character input in an array in java.....how?????
hmm.. its quite easy. I think you guys are familiar with Scanner class. But there is no method in Scanner class to take char input. So you are facing difficulties, right?
okay here I will show you two ways, how to take char as a input in Java.
you may feel it is difficult first time, but it will work nicely. Okay. so first now get first One _
you guys are also familiar about System.out.println(), right? but do you know there is something like System.in.read()?
when you'll learn Java I/O, you will be clear about it. dont worry here, I'll try to give you some idea.
System.in.read() takes input from keyboard. But it has a problem. if you press Return Key (enter), it will take return key as a input. So there should a way to bypass the return key. Okay?
Now better look through the code _
I think there is some unknown part for you. like throws IOException.
Okay dont worry. Just use it. We will discuss about it later.
btw if any problem let me know..
and I'll post the second way of taking char input later if you dont understand it.
cheers!!
okay here I will show you two ways, how to take char as a input in Java.
you may feel it is difficult first time, but it will work nicely. Okay. so first now get first One _
you guys are also familiar about System.out.println(), right? but do you know there is something like System.in.read()?
when you'll learn Java I/O, you will be clear about it. dont worry here, I'll try to give you some idea.
System.in.read() takes input from keyboard. But it has a problem. if you press Return Key (enter), it will take return key as a input. So there should a way to bypass the return key. Okay?
Now better look through the code _
- Code:
import java.io.IOException;
public class ArrayInJava {
public static void main(String[] args) throws IOException {
char[] myArray = new char[3];
System.out
.println("Eneter your characters and press enter after every characters");
for (int i = 0; i < myArray.length; i++) {
myArray[i] = getChar();
}
System.out.println("your chacters are: ");
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + ",");
}
}
public static char getChar() throws IOException {
char ch = (char) System.in.read();
byPassReturnKey();
return ch;
}
public static void byPassReturnKey() throws IOException {
while ((char) System.in.read() != '\n')
;
}
}
I think there is some unknown part for you. like throws IOException.
Okay dont worry. Just use it. We will discuss about it later.
btw if any problem let me know..
and I'll post the second way of taking char input later if you dont understand it.
cheers!!
Last edited by bit0112-rokon on Wed Aug 18, 2010 11:49 pm; edited 1 time in total
_________________________________________________________________
Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী

BIT0112-Rokon- Programmer

- Course(s):
- BIT
Blood Group: O+
Posts: 673
Points: 1269

Re: character input in an array in java.....how?????
bit0112-rokon wrote:hmm.. its quite easy. I think you guys are familiar with Scanner class. But there is no method in Scanner class to take char input. So you are facing difficulties, right?
okay here I will show you two way, how to take char as a input in Java.
you may feel it is difficult first time, but it will work nicely. Okay. so first now get first One _
you guys are also familiar about System.out.println(), right? but do you know there is something like System.in.read()?
when you'll learn Java I/O, you will be clear about it. dont worry here, I'll try to give you some idea.
System.in.read() takes input from keyboard. But it has a problem. if you press Return Key (enter), it will take return key as a input. So there should a way to bypass the return key. Okay?
Now better look through the code _
- Code:
import java.io.IOException;
public class ArrayInJava {
public static void main(String[] args) throws IOException {
char[] myArray = new char[3];
System.out
.println("Eneter your characters and press enter after every characters");
for (int i = 0; i < myArray.length; i++) {
myArray[i] = getChar();
}
System.out.println("your chacters are: ");
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + ",");
}
}
public static char getChar() throws IOException {
char ch = (char) System.in.read();
byPassReturnKey();
return ch;
}
public static void byPassReturnKey() throws IOException {
while ((char) System.in.read() != '\n')
;
}
}
I think there is some unknown part for you. like throws IOException.
Okay dont worry. Just use it. We will discuss about it later.
btw if any problem let me know..
and I'll post the second way of taking char input later if you dont understand it.
cheers!!
Sigh...
try to give a solution that will help them use what they already know, not something that will make them use something they have absolutely no idea about.
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: character input in an array in java.....how?????
topic merged
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: character input in an array in java.....how?????
BIT0122-Amit wrote:
Sigh...
try to give a solution that will help them use what they already know, not something that will make them use something they have absolutely no idea about.
Idea nai, ar mane ai na j, idea hobe na.. taina? and secondly ami bolci.. aita na bujle ami second way ta debo..... and ami firstly 2ta way chinta korci.. tar modde aita best mone hoice.
akhon obosso amr mathai aro onekgula way asce ...
btw tor way ta good.. but not better ...
amr mone hoi nijera torko kore aikhane oder k confused korer kono mane nai.. better j help chaice .. se kototuku bujlo seta beshi important ....
_________________________________________________________________
Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী

BIT0112-Rokon- Programmer

- Course(s):
- BIT
Blood Group: O+
Posts: 673
Points: 1269

Re: character input in an array in java.....how?????
bit0112-rokon wrote:
Idea nai, ar mane ai na j, idea hobe na.. taina? and secondly ami bolci.. aita na bujle ami second way ta debo..... and ami firstly 2ta way chinta korci.. tar modde aita best mone hoice.
akhon obosso amr mathai aro onekgula way asce ...
btw tor way ta good.. but not better ...
amr mone hoi nijera torko kore aikhane oder k confused korer kono mane nai.. better j help chaice .. se kototuku bujlo seta beshi important ....
Exactly what my point is. Ami joddur jani era ekhono onek khani pichaye ache. Exception Handling to aro onek porer bepar. And ami nijei bolsi je amar way ta bujha jabe, but efficient na. jodi ora bujhto tahole read or onnanno method use kora jeto. Kintu keu kichu pore bujhbe ei asha kore advanced jinish bujhano mane onekta class 5 er baccha ke calculus mukhosto korano type er
Jakge.. duitai thak
je jeta valo bujhbe, she sheta korbe 
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: character input in an array in java.....how?????
BIT0122-Amit wrote:here you go:
- Code:
package myPackage;
import java.util.Scanner;
public class ForHabib {
public static void main(String[] args) {
//use Scanner to take input
Scanner myScanner = new Scanner(System.in);
//that's your character array
char[] myArray = new char[3];
for (int i = 0; i < myArray.length; i++) {
//as we don't have any nextChar() method, we are going to take input and keep it in a string
String temp = myScanner.next();
//and then we are going to put the first character of that string to your array
myArray[i] = temp.charAt(0);
}
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
}
}
read the comments for explanation.
Sample Input:
- Code:
ami
bas
qwe
sample output
- Code:
a
b
q
there is also several other methods for doing the same task in more efficient way(for example, bufferreader, or the System.in.read() method) But as you are only a beginner and possibly only know about Scanner till now, use this.
And you better use the code tag next time when you are posting a code.
AND
DO NOT USE ... IN TOPIC TITLE.
why?
1. It looks ugly.
2. It looks horrible.
3. It is what you use in chatting. Are you chatting?
apni Java naming convention maintain koren nai.. bishal oporadh ... ar jonno obossoi apnake shasti pete hobe.....
package name always small letter hoi.. not myPakage, it shoud be mypakage or my.pakage or amit.pakage or amit.bla.bla.bla
note: shasti map kore deya jai.. if amke coffee kkhayer taka dis......
_________________________________________________________________
Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী

BIT0112-Rokon- Programmer

- Course(s):
- BIT
Blood Group: O+
Posts: 673
Points: 1269

Re: character input in an array in java.....how?????
bit0112-rokon wrote:
apni Java naming convention maintain koren nai.. bishal oporadh ... ar jonno obossoi apnake shasti pete hobe.....
package name always small letter hoi.. not myPakage, it shoud be mypakage or my.pakage or amit.pakage or amit.bla.bla.bla
note: shasti map kore deya jai.. if amke coffee kkhayer taka dis......
ki bador re baba
:/ But valo dhorsish :p Rep++
Tor coffee peye jabi.
Note: tor eto gula job er prottektar jonno ekta chicken fry khaoanor pordin nescafe 12 TK coffee pabi.
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: character input in an array in java.....how?????
Often I feel bored to see irrelevant posts in our forum... I came here to help... But unfortunately... I have failed to identify that the problem was solved or not... The solutions are lost inside a dark deep forest of stupid posts...

BIT0102-Mohaimin- Programmer

- Course(s):
- BIT
Blood Group: B+
Posts: 415
Points: 713
Page 1 of 2 • 1, 2 
Page 1 of 2
Permissions in this forum:
You cannot reply to topics in this forum
Active Topics!

» Understanding Timestamp Based Protocol
» Hi from Newbie
» Compare an image from a list of images in the sql database....tell which image is matching
» Sending email without email ID
» teach yourself c by herbert schildt pdf
» .NET MVC3: Unable to find the requested .Net Framework Data Provider. It may not be installed.
» SAD Slides: 4.1, 4.2
» QA topic Presentation - Urgent Notice