How do I input integers in the Terminal?
Hey everybody,
I am having lots of trouble storing values in an array and I want to be prompted at the beginning of each value. The values I will enter will be two digit values such as 23, or 42, or 35.
Anyways, i am prompted but I am only returned the first (char) input into the terminal, obviously because of the Terminal.getchar().· But I need to input two digit integers like I mentioned above.·
This is my code:
import stamp.core.*;
public class IntPrompt {
· public static void main()
· {
··· char c;
··· System.out.println("Begin");
····· int p[noparse]/noparse = new int[noparse][[/noparse]3];
····· for(int i=0; i < 4; i++)
····· {
····· p[noparse][[/noparse]i] = Terminal.getChar();
····· }
····· System.out.print((char) p[noparse][[/noparse]0]);
··· }
}
It seems like there is an easy alternative but my brain is fried.....I can do it using a Scanner in the full version of Java but ahhhh! Has anybody done this before?
Thanks,
Gabe
I am having lots of trouble storing values in an array and I want to be prompted at the beginning of each value. The values I will enter will be two digit values such as 23, or 42, or 35.
Anyways, i am prompted but I am only returned the first (char) input into the terminal, obviously because of the Terminal.getchar().· But I need to input two digit integers like I mentioned above.·
This is my code:
import stamp.core.*;
public class IntPrompt {
· public static void main()
· {
··· char c;
··· System.out.println("Begin");
····· int p[noparse]/noparse = new int[noparse][[/noparse]3];
····· for(int i=0; i < 4; i++)
····· {
····· p[noparse][[/noparse]i] = Terminal.getChar();
····· }
····· System.out.print((char) p[noparse][[/noparse]0]);
··· }
}
It seems like there is an easy alternative but my brain is fried.....I can do it using a Scanner in the full version of Java but ahhhh! Has anybody done this before?
Thanks,
Gabe
Comments
public class IntPrompt {
· public static void main() {
····int v;
··· int p[noparse]/noparse = new int[noparse][[/noparse]3];
··· for(int i=0; i<3; i++) { //read 3 two-digit values
····· System.out.println("Enter two digits: ");
······v = 0;
······for (int j=0; j<2; j++) { //read two digits and convert to binary value
······· v = (v*10) + (Terminal.getChar()-'0');
······}
······System.out.print("\n");
····· p[noparse][[/noparse]i] = v; //store binary value
····}
··· for(int i=0; i<3; i++) { //print stored binary values
····· System.out.print(p[noparse][[/noparse]i]);
····· System.out.print(" ");
··· }
··· System.out.print("\n");
··· while (true) ; //keep javelin alive
· }
}
Note there is no sanity check on input characters.
regards peter
·