Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Multi-Dimentional Array: I can't seem to print out the variables please help.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Multi-Dimentional Array: I can't seem to print out the variables please help.

    import java.util.Scanner;

    public class Scheduler {

    public static final String[] time = { "7:30", "8:30", "9:30", "10:30",
    "11:30", "1:00", "2:00", "3:00", "4:00" };

    public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    String input;
    String[] temp;
    String classTime;
    String day;
    String course;
    String[][] sked;
    int row = 0;
    int col = 0;

    sked = new String[9][6];

    do {
    System.out
    .println("Please input the Course,Day/s, and Time accordingly.");
    input = kb.nextLine();
    if (input.isEmpty()) {
    break;
    }

    temp = input.split(",");
    course = temp[0].toUpperCase();
    classTime = temp[2];
    day = temp[1].toUpperCase();

    if (classTime.matches("[0-9]{1,2}:[0-9]{2}")) {
    row = " 7:30 8:30 9:3010:3011:30 1:00 2:00 3:00 4:00"
    .indexOf(classTime) / 5;

    System.out.println(row);

    }

    // -------------------------days
    // prototype----------------------------------
    /*
    * if(day.matches ("[MTWHFS]{1,6}")){ for (int i = 0; i <
    * day.length(); i++) { col = "MTWHFS".indexOf(day.charAt(i));
    * System.out.println(i); } }else{ System.out.println("Error!"); }
    */

    /*
    * if (day.matches("[MTWHFS]{1,6}")) { for (int i = 0; i <
    * day.length(); i++) { col = "MTWHFS".indexOf(day.charAt(i));
    * System.out.println(i); if (sked == null) { sked [row][col] =
    * course; } else { System.out.println("class is overlapping"); } }
    * }
    */
    // ---------------------------end days prototype...these are
    // failures...------

    if (day.matches("[MTWHFS]{1,6}")) {
    for (int i = 0; i < day.length(); i++) {
    col = "MTWHFS".indexOf(day.charAt(i));
    System.out.println(col);
    }
    } else {
    System.out.println("Error!");
    }

    } while (true);

    // ---------------------printout
    // prototypes.------------------------------------
    /*
    *
    * System.out.println(); System.out.println(
    * "\t\tMonday Tuesday Wednesday Thursday Friday Saturday"
    * ); for (int r=0;r<time.length;r++){ System.out.print(time[r] + "\t");
    * for (int c=0;c<sked[0].length;c++){ if ( sked[r][c] == null ){
    * System.out.print(""); } else { System.out.printf("%-2s",sked[r][c] +
    * "\t\t"); } } System.out.println(); }
    */
    /*
    * System.out.printf("%-5s%10s%10s%10s%10s%10s%10s%n","","M","T","W","H",
    * "F","S"); for (int c = 0; c < sked[0].length; c++) {
    * System.out.printf("%-5s", time[c]);
    *
    * for (int r = 0; r < sked.length; r++) { System.out.printf("%10s",
    * sked[r][c]); }
    *
    * System.out.println(); }
    */
    System.out
    .println("\t\tMonday Tuesday Wednesday Thursday Friday Saturday");
    for (int r = 0; r < time.length; r++) { // top to bottom
    System.out.println(time[r] + "\t");
    for (int c = 0; c < sked[0].length; c++) { // left to right
    if (sked[r][c] == null) {
    System.out.print("");
    } else {
    System.out.printf("%-2s", sked[r][c] + "\t\t");
    }
    // array elements accessed as table[row][col] 
    }
    }

    }
    }


    what's wrong with this piece of code? The things I input won't print out. please help

    newbie here.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Multi-Dimentional Array: I can't seem to print out the variables please help.

    Quote Originally Posted by XxClouDxX
    import java.util.Scanner;
     
    public class Scheduler {
     
    public static final String[] time = { "7:30", "8:30", "9:30", "10:30",
    "11:30", "1:00", "2:00", "3:00", "4:00" };
     
    public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    String input;
    String[] temp;
    String classTime;
    String day;
    String course;
    String[][] sked;
    int row = 0;
    int col = 0;
     
    sked = new String[9][6];
     
    do {
    System.out
    .println("Please input the Course,Day/s, and Time accordingly.");
    input = kb.nextLine();
    if (input.isEmpty()) {
    break;
    }
     
    temp = input.split(",");
    course = temp[0].toUpperCase();
    classTime = temp[2];
    day = temp[1].toUpperCase();
     
    if (classTime.matches("[0-9]{1,2}:[0-9]{2}")) {
    row = " 7:30 8:30 9:3010:3011:30 1:00 2:00 3:00 4:00"
    .indexOf(classTime) / 5;
     
    System.out.println(row);
     
    }
     
    // -------------------------days
    // prototype----------------------------------
    /*
    * if(day.matches ("[MTWHFS]{1,6}")){ for (int i = 0; i <
    * day.length(); i++) { col = "MTWHFS".indexOf(day.charAt(i));
    * System.out.println(i); } }else{ System.out.println("Error!"); }
    */
     
    /*
    * if (day.matches("[MTWHFS]{1,6}")) { for (int i = 0; i <
    * day.length(); i++) { col = "MTWHFS".indexOf(day.charAt(i));
    * System.out.println(i); if (sked == null) { sked [row][col] =
    * course; } else { System.out.println("class is overlapping"); } }
    * }
    */
    // ---------------------------end days prototype...these are
    // failures...------
     
    if (day.matches("[MTWHFS]{1,6}")) {
    for (int i = 0; i < day.length(); i++) {
    col = "MTWHFS".indexOf(day.charAt(i));
    System.out.println(col);
    }
    } else {
    System.out.println("Error!");
    }
     
    } while (true);
     
    // ---------------------printout
    // prototypes.------------------------------------
    /*
    *
    * System.out.println(); System.out.println(
    * "\t\tMonday Tuesday Wednesday Thursday Friday Saturday"
    * ); for (int r=0;r<time.length;r++){ System.out.print(time[r] + "\t");
    * for (int c=0;c<sked[0].length;c++){ if ( sked[r][c] == null ){
    * System.out.print(""); } else { System.out.printf("%-2s",sked[r][c] +
    * "\t\t"); } } System.out.println(); }
    */
    /*
    * System.out.printf("%-5s%10s%10s%10s%10s%10s%10s%n","","M","T","W","H",
    * "F","S"); for (int c = 0; c < sked[0].length; c++) {
    * System.out.printf("%-5s", time[c]);
    *
    * for (int r = 0; r < sked.length; r++) { System.out.printf("%10s",
    * sked[r][c]); }
    *
    * System.out.println(); }
    */
    System.out
    .println("\t\tMonday Tuesday Wednesday Thursday Friday Saturday");
    for (int r = 0; r < time.length; r++) { // top to bottom
    System.out.println(time[r] + "\t");
    for (int c = 0; c < sked[0].length; c++) { // left to right
    if (sked[r][c] == null) {
    System.out.print("");
    } else {
    System.out.printf("%-2s", sked[r][c] + "\t\t");
    }
    // array elements accessed as table[row][col] 
    }
    }
     
    }
    }
    I've put it in coding format so I can read it better.

    I did notice something odd.

    It seems you've got a do.....while(true) loop that of course will never end as long as the program runs.

    Hence it'll never get to the printing out part.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    XxClouDxX (December 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Multi-Dimentional Array: I can't seem to print out the variables please help.

    Any good suggestions then? This program was supposed to print out what index the day and the time is, and then print a table of it. The purpose of the do-while loop there is for the user to input as many subjects, and when the user, inputs nothing, the table prints out.

    my expected output:
    PHP Code:
    Please input the Course,Day/s, and Time accordingly:
    IT121,F,7:30

    Please input the Course
    ,Days/and Time accordingly:
    PE2,S,9:30

    Please input the Course
    ,Day/and Time accordingly:


            
    Monday           Tuesday             Wednesday            Thursday           Friday           Saturday


    7
    :30                                                                                  IT121
    8
    :30
    9
    :30                                                                                                   PE2
    10
    :30
    11
    :30
    1
    :00
    2
    :00
    3
    :00
    4
    :00 

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Multi-Dimentional Array: I can't seem to print out the variables please help.

    Well, did you ever initialize that 2D String array? I mean the values. Unless it's a primitive type, when you refer to the values at the indexes, you'll be getting Null Pointer Exceptions even if you did get to the printing out part.

    Also, that break statement stopped it from being an infinite loop, though you could have put

    do.....while(!input.isEmpty()) instead and left out the break statement.

    I was able to get something similar to what you have above though it's not printing out the class like your example above is.

    As for that, I think it likely you're not setting those values. I'm assuming that's what your sked array is being used for.

    On second thought, keep the break in there. Otherwise it'll be throwing Index Out of Bounds Exceptions I think as it will be referring to null input.
    Last edited by javapenguin; December 19th, 2011 at 08:44 PM.

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Multi-Dimentional Array: I can't seem to print out the variables please help.

    I think maybe it's because it has sked[0].length in the for loop perhaps but I was able to, with the code below:

       import java.util.Scanner;
     
       public class Scheduler {
     
          public static final String[] time = { "7:30", "8:30", "9:30", "10:30",
             "11:30", "1:00", "2:00", "3:00", "4:00" };
     
          public static void main(String[] args) {
             Scanner kb = new Scanner(System.in);
             String input = "";
             String[] temp;
             String classTime = "";
             String day = "";
             String course = "";
             String[][] sked;
             int row = 0;
             int col = 0;
     
             sked = new String[9][6];
             for (int i =0; i < sked.length; i++)
             {
                for (int j =0; j < sked[i].length; j++)
                   sked[i][j] = "";
             }
     
     
             do {
     
                System.out
                   .println("Please input the Course,Day/s, and Time accordingly.");
                input = kb.nextLine();
                if (input.isEmpty()) {
                   break;
                }
     
                temp = input.split(",");
                course = temp[0].toUpperCase();
     
                day = temp[1].toUpperCase();
     
                if (classTime.matches("[0-9]{1,2}:[0-9]{2}")) {
                   row = " 7:30 8:30 9:3010:3011:30 1:00 2:00 3:00 4:00"
                      .indexOf(classTime) / 5;
     
                   System.out.println(row);
     
                }
     
             // -------------------------days
             // prototype----------------------------------
             /*
             * if(day.matches ("[MTWHFS]{1,6}")){ for (int i = 0; i <
             * day.length(); i++) { col = "MTWHFS".indexOf(day.charAt(i));
             * System.out.println(i); } }else{ System.out.println("Error!"); }
             */
     
             /*
             * if (day.matches("[MTWHFS]{1,6}")) { for (int i = 0; i <
             * day.length(); i++) { col = "MTWHFS".indexOf(day.charAt(i));
             * System.out.println(i); if (sked == null) { sked [row][col] =
             * course; } else { System.out.println("class is overlapping"); } }
             * }
             */
             // ---------------------------end days prototype...these are
             // failures...------
     
                if (day.matches("[MTWHFS]{1,6}")) {
                   for (int i = 0; i < day.length(); i++) {
                      col = "MTWHFS".indexOf(day.charAt(i));
                      System.out.println(col);
                      sked[row][col] = course;
                   }
                } 
                else {
                   System.out.println("Error!");
                }
     
             } while (true);
     
          // ---------------------printout
          // prototypes.------------------------------------
          /*
          *
          * System.out.println(); System.out.println(
          * "\t\tMonday Tuesday Wednesday Thursday Friday Saturday"
          * ); for (int r=0;r<time.length;r++){ System.out.print(time[r] + "\t");
          * for (int c=0;c<sked[0].length;c++){ if ( sked[r][c] == null ){
          * System.out.print(""); } else { System.out.printf("%-2s",sked[r][c] +
          * "\t\t"); } } System.out.println(); }
          */
          /*
          * System.out.printf("%-5s%10s%10s%10s%10s%10s%10s%n","","M","T","W","H",
          * "F","S"); for (int c = 0; c < sked[0].length; c++) {
          * System.out.printf("%-5s", time[c]);
          *
          * for (int r = 0; r < sked.length; r++) { System.out.printf("%10s",
          * sked[r][c]); }
          *
          * System.out.println(); }
          */
             System.out
                .println("\t\tMonday Tuesday Wednesday Thursday Friday Saturday");
             for (int r = 0; r < time.length; r++) { // top to bottom
                System.out.println(time[r] + "\t");
                for (int c = 0; c < sked[0].length; c++) { // left to right
                   if (sked[r][c] == null) {
                      System.out.print("");
                   } 
                   else {
                      System.out.printf("%-2s", sked[r][c] + "\t\t");
                   }
                // array elements accessed as table[row][col] ?
                }
             }
     
          }
       }

    get this

     

    Please input the Course,Day/s, and Time accordingly.
    IT191,MTWHF,7:00
    0
    1
    2
    3
    4
    Please input the Course,Day/s, and Time accordingly.
    IT191,MW,7:30
    0
    2
    Please input the Course,Day/s, and Time accordingly.
    IT195,T,8:00
    1
    Please input the Course,Day/s, and Time accordingly.
    PEN193,S,8:00
    5
    Please input the Course,Day/s, and Time accordingly.

    Monday Tuesday Wednesday Thursday Friday Saturday
    7:30
    IT191 IT195 IT191 IT191 IT191 PEN193 8:30
    9:30
    10:30
    11:30
    1:00
    2:00
    3:00
    4:00





    It was a bit neater than that output shown above but it still had the 7:30 up there in a weird spot and all the classes set to the same time period.
    Last edited by javapenguin; December 19th, 2011 at 09:05 PM.

Similar Threads

  1. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 07:50 PM
  2. [SOLVED] 2 dimentional boolean array problems!
    By masterT in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2011, 11:03 PM
  3. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  4. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  5. Help on 2D Multi Table Array
    By clevel211 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 1st, 2011, 03:28 AM