character input in an array in java.....how?????
5 posters
Page 1 of 1
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';
Re: character input in an array in java.....how?????
btw why it is here.. there is a Java help section.. post Java problems there
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
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?
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 Thu Aug 19, 2010 3:49 am; edited 1 time in total
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.
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 ....
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
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......
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.
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 : 715
Re: character input in an array in java.....how?????
BIT0102-Mohaimin wrote: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...
It is called discussion.
And in case you didn't notice, the questioner invoked the thanks button on my post, though he didn't feel it necessary to mark this topic as solved.
The deep blue color of a reply indicates that the topic starter thanked that post.
The discussion occurred after the agreeable solution according to the questioner was posted.
Re: character input in an array in java.....how?????
First of all java.util.Scanner is NOT designed to read primitive data types. It reads a stream of bytes token by token depending on delimiters (default delimiter is space or new line) which CAN BE converted to other data types. You can see this for detail.
One way to read char is using java.lang.InputStream. you need not create any object of this type... An object of InputStream is declared public static final in java.langSystem. Guess what... yeah... System.in
Here is a code... Only the main method
You must handle that java.io.IOException
One way to read char is using java.lang.InputStream. you need not create any object of this type... An object of InputStream is declared public static final in java.langSystem. Guess what... yeah... System.in
Here is a code... Only the main method
- Code:
public static void main(String[] args) {
char[] arr = new char[10];
for (int i = 0; i < arr.length; i++) {
try {
arr[i] = (char)System.in.read();
// It reads the unicode value (Java... so, NOT ASCII value), you need to convert to char
} catch (IOException e) {
e.printStackTrace();
}
}
}
You must handle that java.io.IOException
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Similar topics
» How to Take input in java ?. Specially character input..
» Java EE vs .Net???
» Java 7
» About Java 3D
» Java LAF(look and feel)
» Java EE vs .Net???
» Java 7
» About Java 3D
» Java LAF(look and feel)
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford