Storing prompted values in array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grbeltran
    New Member
    • Oct 2006
    • 6

    #1

    Storing prompted values in array.

    Hey everybody,

    I am having lots of trouble storing values in an array and I want to be prompt 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 there is problem with my loop or (char) print statements but i cant get it to work!!!

    This is my code:

    Code:
    class prompt2 { 
         public static void main(String[] args) 
              throws java.io.IOException {
    			
               int p[] = new int[4];
    	       
                      for ( int i = 0; i <  4; i++)
    	 {
    	  System.out.println("Enter the distance from the center to point p" + i + ":\n");
    	  p[i] = System.in.read() ;
    				
    	  System.out.println("The numbers in the array for p are:  p0 is: " + p[0] + " p1 is: " + (char) p[1]+ " p2 is: " + 
    				(char) p[2]+ " p3 is: " + (char) p[3]);


    Any help is very much appreciated!

    Thanks,

    Gabe
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by grbeltran
    Hey everybody,

    I am having lots of trouble storing values in an array and I want to be prompt 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 there is problem with my loop or (char) print statements but i cant get it to work!!!

    This is my code:

    Code:
    class prompt2 { 
    public static void main(String[] args) 
    throws java.io.IOException {
     
    int p[] = new int[4];
     
    for ( int i = 0; i < 4; i++)
    	 {
    	 System.out.println("Enter the distance from the center to point p" + i + ":\n");
    	 p[i] = System.in.read() ;
     
    	 System.out.println("The numbers in the array for p are: p0 is: " + p[0] + " p1 is: " + (char) p[1]+ " p2 is: " + 
    				(char) p[2]+ " p3 is: " + (char) p[3]);


    Any help is very much appreciated!

    Thanks,

    Gabe
    Code:
     
     
    Use a scanner:
    import java.util.Scanner;
    class prompt2 {
    	 public static void main(String[] args)
    		  throws java.io.IOException {
       Scanner scan = new Scanner(System.in);
    		   int p[] = new int[4];
    		   for ( int i = 0; i <  4; i++) {
    	  System.out.print("Enter the distance from the center to point p" + i + " :");
    		p[i] = scan.nextInt() ;
    		System.out.println();
       }
       System.out.println("The numbers in the array for p are:  p0 is: " + p[0] + " p1 is: " + p[1]+ " p2 is: " +
       p[2]+ " p3 is: " + p[3]);
      }
     }
    Why were you typecasting to char?

    Comment

    • grbeltran
      New Member
      • Oct 2006
      • 6

      #3
      Wow!!!! I never heard of scanners but it worked!!

      Thank you!! TY very much!

      Now, are u by chance familiar with the Javelin?

      Thanks,

      gabe

      Comment

      • grbeltran
        New Member
        • Oct 2006
        • 6

        #4
        Wow!!!! I never heard of scanners but it worked!!

        Thank you!! TY very much!

        Now, are u by chance familiar with the Javelin?

        Thanks,

        gabe


        Originally posted by r035198x
        Code:
         
         
        Use a scanner:
        import java.util.Scanner;
        class prompt2 {
        	 public static void main(String[] args)
        		  throws java.io.IOException {
           Scanner scan = new Scanner(System.in);
        		   int p[] = new int[4];
        		   for ( int i = 0; i <  4; i++) {
        	  System.out.print("Enter the distance from the center to point p" + i + " :");
        		p[i] = scan.nextInt() ;
        		System.out.println();
           }
           System.out.println("The numbers in the array for p are:  p0 is: " + p[0] + " p1 is: " + p[1]+ " p2 is: " +
           p[2]+ " p3 is: " + p[3]);
          }
         }
        Why were you typecasting to char?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by grbeltran
          Wow!!!! I never heard of scanners but it worked!!

          Thank you!! TY very much!

          Now, are u by chance familiar with the Javelin?

          Thanks,

          gabe
          Javelin the sport?

          Comment

          • grbeltran
            New Member
            • Oct 2006
            • 6

            #6
            Originally posted by r035198x
            Javelin the sport?
            no, sorry.. the java based microcontroller from Parallax.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by grbeltran
              no, sorry.. the java based microcontroller from Parallax.
              What does it do and why did you ask about it?

              Comment

              • grbeltran
                New Member
                • Oct 2006
                • 6

                #8
                Originally posted by r035198x
                What does it do and why did you ask about it?
                Nah its just the microcontroller i am working with on a project of mine. Its programming language is a subset of Javas

                Comment

                Working...