+ 1

How to get char type variable value from user in java???

I'm learning java on SoloLearn but I dont understand how get value from user ??

11th Jul 2019, 11:13 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
17 ответов
+ 1
use this. because next is returning string, you can use charAt to get the first character scanner.next().charAt(0) https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
11th Jul 2019, 11:30 AM
Taste
Taste - avatar
0
Taste What if I use an array of 30 characters how to get values for this from user. The following example is in cpp i.e char name[30]; cin.getline (name, 30);
11th Jul 2019, 11:42 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
you can use nextLine() instead of next(). it'll return String. but if you want the array you can use toCharArray() on its result. scanner.nextLine().toCharArray()
11th Jul 2019, 11:46 AM
Taste
Taste - avatar
0
I think, It will exceed the length
11th Jul 2019, 11:52 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
next also accept a regex pattern as a parameter. maybe something like ^.{30}$ will do the trick
11th Jul 2019, 11:58 AM
Taste
Taste - avatar
0
Please, write a full code!! Im a little bit confused
11th Jul 2019, 12:02 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
scanner.next("^.{30}
quot;);
11th Jul 2019, 12:05 PM
Taste
Taste - avatar
0
Will you pls elaborate this
12th Jul 2019, 3:48 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
regex in use means any character up to 30. it'll limit the input to to 30 character
12th Jul 2019, 3:58 AM
Taste
Taste - avatar
0
If user press enter key on 15 index of an array than what happen
12th Jul 2019, 4:03 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
it works like getline. it'll take an input but only limited to 30 character. the only difference is its rturning string not array. but there are workaround like i show above
12th Jul 2019, 4:07 AM
Taste
Taste - avatar
0
Okay!! I asked one more question that what is the alternate of goto statment in java
12th Jul 2019, 4:09 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
i think james gosling completly remove that when designin java. because its not exist, even in c++ community many peope think goto is a bad practice. even if goto doesnt exist, but the label does. as far as i know its only use in continue. the useage is pretty similar, but the main function is different. the normal goto used to jump to unconditionally jump to other part of the code. meanwhile continue with label can be use to skip the outer loop from inner loop. outer: for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ if(j%2==0)continue outer; System.out.println(i+"="+j); } }
12th Jul 2019, 4:27 AM
Taste
Taste - avatar
0
The way they work, I think they are both similar
12th Jul 2019, 5:13 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
yes, but its limited to a loop only. and again same with goto in c++, many people dont like that.
12th Jul 2019, 5:17 AM
Taste
Taste - avatar
0
Why?? What if a programmer encounter the following situation where he/she need to goto back to take value again like back: cin>>temp; if (temp.length() < 6 && temp.length () > 15) { cout <<"Something went wrong, Try again!!"; goto back; } else cout <"Welcome"; In cpp we simply use goto statment but what if we doing a project in java
12th Jul 2019, 5:37 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
do{ if(temp.length()>=6 && temp.length()<=15) break; //output error }while(true); //output welcome
12th Jul 2019, 5:43 AM
Taste
Taste - avatar