wood burning stoves*
The moose likes Beginning Java and the fly likes Trying to print a 2-dimensional table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to print a 2-dimensional table" Watch "Trying to print a 2-dimensional table" New topic
Author

Trying to print a 2-dimensional table

deb uebersetzig
Greenhorn

Joined: Sep 29, 2000
Posts: 6
I am trying to print a value from the table,I am not coverting my Char and I can't get a clean compile...what am I doing wrong?
public class Schedule
{
public static void main(String[] args) throws Exception
{
String[][] id =
{ {"CIS 115 Th 1:30"},
{"CIS 120 Mo 10:30"},
{"CIS 125 We 9:30"},
{"CIS 130 Mo 8:30"} };

char course;
int x, y, courseId;
System.out.print("Enter course ID-number 0-3, ");
course = (char)System.in.read();
for(x = 0; x < 4; ++x)
for(y = 0; y < 1; ++y)
courseId = Character.getNumericValue(course);
if(courseId == id[x][y])
System.out.println("The course name, day and time are " + id[x][y]);
}
}
greg clarke
Greenhorn

Joined: Oct 06, 2000
Posts: 25
Deb
The compile error is because you are comparing an int to a String in the line: if ( courseId == id[x][y] ). The compile error tries to explain that in its own way.
Your code compiled when I replace id[x][y] with 1 (any int would do) and when I initialised courseId and y to 0. I know this doesn't make the program work properly, but it shows what is causing the compile error.
I'm still not sure what your algorithm is exactly, though it seems to me you want to index the course information to retrieve it by course id. If so, you may need to look at your array design.
For example, if the array index is the course id, then you only need a single dimensional array. And in fact you only initialise one dimension of the array anyway.
(You could also look at a collection type that better suits your program, but that may cloud the issue at this stage. And its probably better to come to terms with arrays first.)
Let us know how it goes.
 
 
subject: Trying to print a 2-dimensional table
 
Similar Threads
problem in one-to-many mapping
Operators
Convert 1d array to 2D boolean
how to compare the values in arrays
Neef Help with Printing