• Post Reply Bookmark Topic Watch Topic
  • New Topic

Limit on String Input from Console  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am trying to make a program in which first I am entering number of charachters and then in nextline their is exactly that number of characters should be enter after than program should stop taking input from console

this is I have try so far


Thanks in advance
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you planning to do? You said you want to add characters but you are then using next(). Have you read what next() does? And why are you going on about characters and using a List of Strings?

Don't add those characters individually to the List like that. Consider a loop for adding. And set the List's capacity in its constructor call.
Also move all that code out of the main method and expunge the word static from your vocabulary.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What are you planning to do?.

I am planning to take some Integer from console an then in next line take only that length character(e.g if 5 then will read only 5 charcters)

You said you want to add characters but you are then using next(). Have you read what next() does?

More than charcters I want to read a String from console but only that length that is specificed in first line



And why are you going on about characters and using a List of Strings?

these are some charcters that I stored for further some calculation in my programs

Don't add those characters individually to the List like that. Consider a loop for adding. And set the List's capacity in its constructor call.
Also move all that code out of the main method

will try what you are suggesting

and expunge the word static from your vocabulary.

can you explain what it mean?I am not able to understand it?

Thanks for quick reply
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you start writing static all over your code you move out of the realms of object‑oriented programming.
You know that Scanner#next reads the next “complete token”, so you cannot use Scanner#next to read a token of a specified length. You can count the lengths of tokens and continue requesting until you reach your required length. You can append a token to a StringBuilder(←link) and query its length. There is even a method in StringBuilder which sets it to a particular length.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote: . . . these are some charcters that I stored for further some calculation in my programs . . .
Well, they look like Strings to me. If you want a List<Character> use a List<Character>. Because of autoboxing you can add chars to that List directly.
Thanks for quick reply
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise that chars are not letters but numbers, so you can use them in a loop?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell

I think you are talking about method

StringBuilder(int capacity)



I am not much more experience in StringBuilder can you write a snap of code

If I want to read only 6 charcters from console input and after 6 charcters user can't type any word.?How can i achieve this?(Code please?)
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote: . . .
If I want to read only 6 charcters from console input and after 6 charcters user can't type any word.? . . .
Not, as far as I know, possible with a Scanner.Setting the length is simply a performance thing; I think a StringBuilder defaults to 64 chars. It is setting the length after filling that is what you want. Note you can empty a StringBuilder by setting its length to 0.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell
Thanks

what I have done is I have store all input in StringBuilder and then I have replace all String with
""
from where I do not want in my program using replace method of StringBuilder class
and then I did sb.toSting().trim().

It works for me
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done but I think there is a simpler way to curtail a string builder.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome but there still is a simpler technique
sb.setLength(999);

Edit: change s.set... to sb.set...
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . .
builder.add("Campbell").("Ritchie").("Kishor").("Joshi"); // method chaining
. . .
Good grief! What a mistake I made. Sorry.
There isn't an add method. And I only called the method once. Ouch! That would never have compiled That should have been
builder.append("Campbell").append("Ritchie").append("Kishor").append("Joshi"); // method chaining
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya I have also didn't find any add method.But i got your point
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!