• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Trying to print a 2-dimensional table

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]);
}
}
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic