• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops and Input  RSS feed

 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


well, what i want is, if i input 3,
it'll be :

1 name : sdhkdjsk dfkfhsdkfj ( i can write anything, till i enter)
2 name : fdsjkfhfjkdskf dfjsk
3 name : dhfdjskfhj dfjkshfjhd

Can you help me?
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Gabrielle,

In this line


for int types of values, you have to use nextInt() method.

you must write
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle Linkherz wrote:

well, what i want is, if i input 3,
it'll be :

1 name : sdhkdjsk dfkfhsdkfj ( i can write anything, till i enter)
2 name : fdsjkfhfjkdskf dfjsk
3 name : dhfdjskfhj dfjkshfjhd

Can you help me?
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:Hello Gabrielle,

In this line


for int types of values, you have to use nextInt() method.

you must write


Thank you.. but, it's still wrong . Watch out my edited code .. (That i quote in comments since i didn't know how to edit it)
The output will be :
1 name :
2 name : sdkf dfdsf
3 name : kjdks dfkjdks

so, i can't input 1 name .. Why?
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to take input from the users means if you want to take names from the user you have to use String array

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:. . . if you want to take names from the user you have to use String array
. . .
No you don't. YOu could use a List instead.

Besides there is a problem I can see which you have missed. I also know where there is an explanation why the first name comes out as empty. It's here.

GL: What does it say about nextLine in your book?
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle,
you wrote wrong syntax here in this line


here closing round bracket should not be there
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Sankalp Bhagat wrote:. . . if you want to take names from the user you have to use String array
. . .
No you don't. YOu could use a List instead.

Besides there is a problem I can see which you have missed. I also know where there is an explanation why the first name comes out as empty. It's here.

GL: What does it say about nextLine in your book?


Okay Sir. Thank you!
But I've read in some book that if the size is known we better should use array instead of list because arrays are faster than list because they are the part of the language and the list say ArrayList is the part of API
and when the default capacity of the ArrayList is filled, internally a new ArrayList is created and the old one gets copied into that.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you should always start like this for a for loop:-
for (int i = 0; i < myArray.length; i++) ...
Always start thinking about for loops like that, even though you may need to change the format later. Those values for i correspond exactly to the indices in that array.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, you should always start like this for a for loop:-
for (int i = 0; i < myArray.length; i++) ...
Always start thinking about for loops like that, even though you may need to change the format later. Those values for i correspond exactly to the indices in that array.


But Sir this wouldn't be an optimized code, would it be? Because everytime when the value of "i" will be incremented, "myArray.length" will be calculated.
if the loop is iterating for 1000 times, then will be calculated then it will be compared to "i"
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:If you want to take input from the users means if you want to take names from the user you have to use String array



it's work better than mine ... but,

if when i compile, it'll show:
1 name :
2 name : fdkfjdks
3 name : dfkjdk

i can't input 1 name
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if we write it's a one time calculation.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle Linkherz wrote:
Sankalp Bhagat wrote:If you want to take input from the users means if you want to take names from the user you have to use String array



it's work better than mine ... but,

if when i compile, it'll show:
1 name :
2 name : fdkfjdks
3 name : dfkjdk

i can't input 1 name


You don't have to press "Enter" key in this line while giving input Enter a name : write in here itself like

Enter a name : Gabrielle
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:
Gabrielle Linkherz wrote:
Sankalp Bhagat wrote:If you want to take input from the users means if you want to take names from the user you have to use String array



it's work better than mine ... but,

if when i compile, it'll show:
1 name :
2 name : fdkfjdks
3 name : dfkjdk

i can't input 1 name


You don't have to press "Enter" key in this line while giving input Enter a name : write in here itself like

Enter a name : Gabrielle


well, but in my program :

enter a name :
enter a name : sdkfj

can't input 1st name
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:myArray.length" will be calculated

Why will it be calculated ? length is a property.

 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Sankalp Bhagat wrote:myArray.length" will be calculated

Why will it be calculated ? length is a property.



if that me.length is in loop that iterate for 1000 times, then thousand times it will be calculated what's the length of me in each iteration
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabrielle Linkherz wrote:
Sankalp Bhagat wrote:
Gabrielle Linkherz wrote:
Sankalp Bhagat wrote:If you want to take input from the users means if you want to take names from the user you have to use String array



it's work better than mine ... but,

if when i compile, it'll show:
1 name :
2 name : fdkfjdks
3 name : dfkjdk

i can't input 1 name


You don't have to press "Enter" key in this line while giving input Enter a name : write in here itself like

Enter a name : Gabrielle


well, but in my program :

enter a name :
enter a name : sdkfj

can't input 1st name


You can't input 1 name because of the Scanner class nextLine() method.
I don't know why but if you use nextInt() method and then nextLine() method it behaves wierdly
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:
You can't input 1 name because of the Scanner class nextLine() method.
I don't know why but if you use nextInt() method and then nextLine() method it behaves wierdly


Nothing weird about it. The nextInt() method returns an integer token and leaves the cursor at the end of the integer. The nextLine() method returns a line (as a token) and leaves the cursor after the carriage return.

When the user is first asked to enter a number, he/she will enter a number and press a carriage return. So, the nextInt() will read the number, and the following nextLine() will consume the carriage return that is after it.


The only thing "weird" about it is that you expect the nextInt() to consume stuff after the number -- and there is no reason why it should.

Henry
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Sankalp Bhagat wrote:
You can't input 1 name because of the Scanner class nextLine() method.
I don't know why but if you use nextInt() method and then nextLine() method it behaves wierdly


Nothing weird about it. The nextInt() method returns an integer token and leaves the cursor at the end of the integer. The nextLine() method returns a line (as a token) and leaves the cursor after the carriage return.

When the user is first asked to enter a number, he/she will enter a number and press a carriage return. So, the nextInt() will read the number, and the following nextLine() will consume the carriage return that is after it.


The only thing "weird" about it is that you expect the nextInt() to consume stuff after the number -- and there is no reason why it should.

Henry


Thank you. I didn't know that why it behaves like that
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:...if that me.length is in loop that iterate for 1000 times, then thousand times it will be calculated what's the length of me in each iteration

Again, Why are you calling it as "Calculated" ?
In simplest terms, it is "read" not calculated.

The value of a variable may be read 10 times, 1000 times, how does it matter ?
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Sankalp Bhagat wrote:...if that me.length is in loop that iterate for 1000 times, then thousand times it will be calculated what's the length of me in each iteration

Again, Why are you calling it as "Calculated" ?
In simplest terms, it is "read" not calculated.

The value of a variable may be read 10 times, 1000 times, how does it matter ?


JVM calculates that. That's why I said calculated
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying that
Is different as opposed toor even
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably it would be possible to change the default delimiter of a Scanner to only work against a new line for the System.
That way you could just use next() instead of nextLine()?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:. . . this wouldn't be an optimized code, would it be?
Who cares about the 0.01μs it takes to fetch myArray.length when you are taking 5s to enter the name via the keyboard?
You can introduce logic or runtime errors into code by micro‑optimisations like that, for no actual benefit in performance.
Because everytime when the value of "i" will be incremented, "myArray.length" will be calculated. . . .
That increases the correctness of the code. If you manage to replace the array with one of a different length somehow, the loop will continue to run without Exceptions. The value of .length is not calculated. It is a final field of the array and therefore needs only to be fetched.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Are you saying that
Is different as opposed toor even



What do you think which is the optimized code in these below two codes?

first code :


second code:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:. . .

Thank you. I didn't know that why it behaves like that
That is what the API Documentation says
This method returns the rest of the current line …
The real problem is that there are so few tutorials which explain that, and I have yet to see a good explanation of nextLine in a book.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:. . .
What do you think which is the optimized code in these below two codes?
. . .
Who cares which is optimised? Before asking that sort of thing please inspect the bytecode for those two code snippets. Why do you think we are interested in the 0.01μs you are going to save when your 7 print calls may take as long as 0.1s?

We are interested in which is the correct code. You need to confirm that there is no risk of an additional line introducing any errors under any circumstances.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two things in perspective here...
Either you are referring to method or a property

x.length is a property (wont give much difference than value)
x.length() is a method (depends on method implementation)

By the way, Strings are immutable, here is implementation of String.java:


 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Presumably it would be possible to change the default delimiter of a Scanner to only work against a new line for the System.
That way you could just use next() instead of nextLine()?
Easy. Yes. You can even follow it with a reset call on the Scanner object which reverts to the default delimiter. But you have to use an Integer.parseInt() call to get an int out of it. Swings and roundabouts.

I would prefer nextInt followed by a “blank” nextLine call.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:Presumably it would be possible to change the default delimiter of a Scanner to only work against a new line for the System.
That way you could just use next() instead of nextLine()?
Easy. Yes. You can even follow it with a reset call on the Scanner object which reverts to the default delimiter. But you have to use an Integer.parseInt() call to get an int out of it. Swings and roundabouts.

I would prefer nextInt followed by a “blank” nextLine call.


You don't have to use parseInt.
nextInt() will still work.
At least, I don't see why not.

nextInt() reads up to the next token, whatever that token is.

I will say, this would work with a command line, but not necessarily with an IDE console. There's no guarantee the IDE uses the System lineSeparator.

Just a quick check:

This works as expected on a Windows command line.
And I was right about the IDE. IntelliJ just sits there.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:. . . nextInt() will still work.
. . .
Yes, you are right. Sorry for my mistake. You would of course have to ensure the enter key immediately follows the input.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:. . . nextInt() will still work.
. . .
Yes, you are right. Sorry for my mistake. You would of course have to ensure the enter key immediately follows the input.


Oh yes, but in these sorts of situations, where Scanner is used for command line input, that's normally a given.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!