• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accepting character values inside for loop  RSS feed

 
Dibyajit Chakraborty
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to input character values from the user inside a for loop. The problem is that it is accepting values in alternate iterations.

In the output, it asks for the grade of the 1st item, and then doesn't ask for the 2nd item. Again it asks for the 3rd but not the 4th and so on... Any help as to why it does this would be appreciated.
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dibyajit Chakraborty,

Beside your identified problem, I'd fix some other problems also.

1. Avoid on-demand imports by specifying with wild card "*". Better use single imports by specifying exact class you're going to use. It makes your code more readable. (I mean line 1)
2. Class names suppose to start with Upper case (line 2)
3. main(String args[]) it is not conventional, suppose to be main(String[] args) - as it is an array of Strings, args it is just a reference variable. I know in some books you could find as it is fine way to do it. In my opinion it is better to stick to a one standard most used by community, and that standard is latter one.
4. Variables s1, s2, s3... are not needed. "for" loop does that job, and it is fully enough to have one variable, let's say "i".
5. Never ever omit "curly" braces around: "if", "else". Never. Also, make sure, body of "if" statement is shifted by 4 spaces to the right.
6. Within "for" loop make some spaces, so you could read it at least, what is written in there.
7. What meanings are for variables "cp", "c1", "cred1", maybe choose something more descriptive.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use data streams for text input or output. Use readers. Read all about it in the Java™ Tutorials. If you use readers (or a Scanner) you cannot directly read a char; you will probably have to use something like myScanner.next().charAt(0);
Don't use the read() method. It is horrible. If you really want to use read() you might as well use System.in.read(). Remember you may need to pass ctrl‑D (*nix) or ctrl‑Z (on Windows) to terminate the input if you use read(). But of course, read() reads every keystroke. If you enter E it reads E but nothing happens until you push the enter key. Then it reads the enter key as an argument to read() and that is the nest iteration of the loop. You can see it if you add the following after line 16:
System.out.printf("You have just entered \u201c%c\u201d%n", grade);
It might be even worse on a Windows box which uses two chars for the enter key. I told you read() was a horrible method.

There is all sorts of poor style and poor design in that class of yours, I am afraid.
 
Dibyajit Chakraborty
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get all you are saying. I know that the method I employed is not a very good one. Its just that I am returning to Java programming (I did some programming in BlueJ previously) after a very long time and want to ensure that I remember the basics. So, I'm going to try and use a much simpler program to really understand what's going on.

This again causes the same problem of taking up 2 iterations. I tried to use Ctrl+z instead of enter, but it resulted in the same problem. So is there no way I can use these statements to input multiple char values?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Campbell has already described why not to use DataInputStream.read(). You can use Scanner as suggested.
2. In line 13 instead of in.read() you can use in.readChar(). Again not recommended though. Use Scanner.
3. Still you are not following the advice of Liutauras Vilda.
Avoid java.io.*;
Use java.io.DataInputStream;
Class name should follow this conventions
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:1. Campbell has already described why not to use DataInputStream.read(). . . .
I actually did slightly more than that. I said not to use DataInputStream and not to use read(). Both are unsuitable for keyboard input. The read() method is intended for reading a binary file, not for text at the keyboard, and DataInputStreams are intended also for binary files. I have suffered the read() method in the past, but at least I learned to avoid it quickly enough!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I ran that program; I changed the loop to use i and changed +a to + i at the end of line 11. I got this sort of result:-
java BUFFERTEST
Enter character no 1
Campbell
Enter character no 2
Enter character no 3
Enter character no 4
Enter character no 5
The last char is b
campbell@campbellsComputer:~/java$ java BUFFERTEST
Enter character no 1
c
Enter character no 2
Enter character no 3
a
Enter character no 4
Enter character no 5
m
The last char is m
If you had an even number of iterations, and if you put quote marks round the offending char in line 14, you would see
The last char is "
"
i.e. a line break.

As I told you, read() is reading the line end characters as one iteration.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!