473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. class prompt2 { 
  2.      public static void main(String[] args) 
  3.           throws java.io.IOException {
  4.  
  5.            int p[] = new int[4];
  6.  
  7.                   for ( int i = 0; i <  4; i++)
  8.      {
  9.       System.out.println("Enter the distance from the center to point p" + i + ":\n");
  10.       p[i] = System.in.read() ;
  11.  
  12.       System.out.println("The numbers in the array for p are:  p0 is: " + p[0] + " p1 is: " + (char) p[1]+ " p2 is: " + 
  13.                 (char) p[2]+ " p3 is: " + (char) p[3]);
  14.  
  15.  


Any help is very much appreciated!

Thanks,

Gabe
Nov 9 '06 #1
7 2289
r035198x
13,262 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. class prompt2 { 
  2. public static void main(String[] args) 
  3. throws java.io.IOException {
  4.  
  5. int p[] = new int[4];
  6.  
  7. for ( int i = 0; i < 4; i++)
  8.      {
  9.      System.out.println("Enter the distance from the center to point p" + i + ":\n");
  10.      p[i] = System.in.read() ;
  11.  
  12.      System.out.println("The numbers in the array for p are: p0 is: " + p[0] + " p1 is: " + (char) p[1]+ " p2 is: " + 
  13.                 (char) p[2]+ " p3 is: " + (char) p[3]);
  14.  
  15.  


Any help is very much appreciated!

Thanks,

Gabe
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Use a scanner:
  4. import java.util.Scanner;
  5. class prompt2 {
  6.      public static void main(String[] args)
  7.           throws java.io.IOException {
  8.    Scanner scan = new Scanner(System.in);
  9.            int p[] = new int[4];
  10.            for ( int i = 0; i <  4; i++) {
  11.       System.out.print("Enter the distance from the center to point p" + i + " :");
  12.         p[i] = scan.nextInt() ;
  13.         System.out.println();
  14.    }
  15.    System.out.println("The numbers in the array for p are:  p0 is: " + p[0] + " p1 is: " + p[1]+ " p2 is: " +
  16.    p[2]+ " p3 is: " + p[3]);
  17.   }
  18.  }
  19.  
  20.  
Why were you typecasting to char?
Nov 9 '06 #2
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
Nov 9 '06 #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


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Use a scanner:
  4. import java.util.Scanner;
  5. class prompt2 {
  6.      public static void main(String[] args)
  7.           throws java.io.IOException {
  8.    Scanner scan = new Scanner(System.in);
  9.            int p[] = new int[4];
  10.            for ( int i = 0; i <  4; i++) {
  11.       System.out.print("Enter the distance from the center to point p" + i + " :");
  12.         p[i] = scan.nextInt() ;
  13.         System.out.println();
  14.    }
  15.    System.out.println("The numbers in the array for p are:  p0 is: " + p[0] + " p1 is: " + p[1]+ " p2 is: " +
  16.    p[2]+ " p3 is: " + p[3]);
  17.   }
  18.  }
  19.  
  20.  
Why were you typecasting to char?
Nov 9 '06 #4
r035198x
13,262 8TB
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?
Nov 9 '06 #5
Javelin the sport?
no, sorry.. the java based microcontroller from Parallax.
Nov 9 '06 #6
r035198x
13,262 8TB
no, sorry.. the java based microcontroller from Parallax.
What does it do and why did you ask about it?
Nov 9 '06 #7
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
Nov 10 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paul C-T | last post by:
Hi, Am I trying to be too clever here? I am trying to write a PHP page to enable me to enter values into a form then write those values to a text file. I want to use the form & table that...
15
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
3
by: Brad | last post by:
I am storing an array which contains about a dozen chracter items to a Session variable. Later, I need to use this array so I am doing the following: Dim eventTypes As String() =...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
8
by: Jay | last post by:
I'm trying to store a sequence of operations and values of different types into a single array. It's a sequence of command word bytes, and a sequence of one or more values (as determined by the...
7
by: pereges | last post by:
I've to store an array of structures: typedef struct { double origin; double e_field_at_origin_real, e_field_at_origin_imag; double direction; double pathlength; int depth; }ray;
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.