Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Trying to print a 2-dimensional table

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);
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]);
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic