+ 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 ??
17 Answers
+ 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()
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);
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()
0
I think, It will exceed the length
0
next also accept a regex pattern as a parameter. maybe something like ^.{30}$ will do the trick
0
Please, write a full code!! Im a little bit confused
0
scanner.next("^.{30}quot;);
0
Will you pls elaborate this
0
regex in use means any character up to 30.
it'll limit the input to to 30 character
0
If user press enter key on 15 index of an array than what happen
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
0
Okay!! I asked one more question that what is the alternate of goto statment in java
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);
}
}
0
The way they work, I think they are both similar
0
yes, but its limited to a loop only.
and again same with goto in c++, many people dont like that.
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
0
do{
if(temp.length()>=6 && temp.length()<=15)
break;
//output error
}while(true);
//output welcome