2

I am working on a lab for school so any help would be appreciated, but I do not want this solved for me. I am working in NetBeans and my main goal is to create a "two-dimensional" array by scanning in integers from a text file. So far, my program runs with no errors, but I am missing the first column of my array. My input looks like:

6
3
0 0 45
1 1 9
2 2 569
3 2 17
2 3 -17
5 3 9999
-1

where 6 is the number of rows, 3 is the number of columns, and -1 is the sentinel. My output looks like:

0 45 
1 9 
2 569 
2 17 
3 -17 
3 9999 
End of file detected.
BUILD SUCCESSFUL (total time: 0 seconds)

As you can see, everything prints correctly except for the missing first column.

Here is my program:

import java.io.*;
import java.util.Scanner;

public class Lab3
{
    public static void main(String[] arg) throws IOException
    {
        File inputFile = new File("C:\\Users\\weebaby\\Documents\\NetBeansProjects\\Lab3\\src\\input.txt");
        Scanner scan = new Scanner (inputFile);
        final int SENT = -1;
        int R=0, C=0;
        int [][] rcArray;  

        //Reads in two values R and C, providing dimensions for the rows and columns.
        R = scan.nextInt();
        C = scan.nextInt();        

        //Creates two-dimensional array of size R and C.
        rcArray = new int [R][C];   

        while (scan.nextInt() != SENT)   
                {            
                    String line = scan.nextLine();
                    String[] numbers = line.split(" ");
                    int newArray[] = new int[numbers.length];

                    for (int i = 1; i < numbers.length; i++)
                    {
                        newArray[i] = Integer.parseInt(numbers[i]);
                        System.out.print(newArray[i]+" ");                        
                    }
                    System.out.println();
                }

                System.out.println("End of file detected.");    
    }

}

Clearly, there is a logical error here. Could someone please explain why the first column is invisible? Is there a way I can only use my rcArray or do I have to keep both my rcArray and newArray? Also, how I can get my file path to just read "input.txt" so that my file path isn't so long? The file "input.txt" is located in my Lab3 src folder (same folder as my program), so I thought I could just use File inputFile = new File("input.txt"); to locate the file, but I can't.

//Edit

Okay I have changed this part of my code:

for (int i = 0; i < numbers[0].length(); i++)
{
  newArray[i] = Integer.parseInt(numbers[i]);                        
  if (newArray[i]==SENT)
     break; 
  System.out.print(newArray[i]+" ");                        
}
System.out.println();

Running the program (starting at 0 instead of 1) now gives the output:

0 
1 
2 
3 
2 
5 

which happens to be the first column. :) I'm getting somewhere!

//Edit 2

In case anyone cares, I figured everything out. :) Thanks for all of your help and feedback.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Kelly Lavertu
  • 23
  • 1
  • 4

2 Answers2

2

Since you do not want this solved for you, I will leave you with a hint:

Arrays in Java are 0 based, not 1 based.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • Thanks for your quick reply. However, using 0 instead of 1 gives me the error 'Exception in thread "main" java.lang.NumberFormatException: For input string: ""' meaning the first scanned object is in fact an empty string and needs to be skipped over. – Kelly Lavertu Mar 08 '13 at 01:25
  • @KellyLavertu Then you need to add an if statement to only print an element of the array when it's not blank. Or maybe even print some spaces for formatting if it is. – Code-Apprentice Mar 08 '13 at 01:26
  • @KellyLavertu But that doesn't make sense with your original input. (None of your rows begin with leading spaces.) See [Jon Skeet's](http://stackoverflow.com/a/15285149/758280) answer for the other mistake in your code. – Jeffrey Mar 08 '13 at 01:26
  • Jeffrey you are right, it didn't make sense. I appreciate everyone's advice! – Kelly Lavertu Mar 08 '13 at 02:10
1

As well as Jeffrey's point around the 0-based nature of arrays, look at this:

while (scan.nextInt() != SENT)   
{            
    String line = scan.nextLine();
    ...

You're consuming an integer (using nextInt()) but all you're doing with that value is checking that it's not SENT. You probably want something like:

int firstNumber;
while ((firstNumber = scan.nextInt()) != SENT)
{
    String line = scan.nextLine();
    ...
    // Use line *and* firstNumber here

Or alternatively (and more cleanly IMO):

while (scan.hasNextLine())
{
    String line = scan.nextLine();
    // Now split the line... and use a break statement if the parsed first
    // value is SENT.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I wish I could +1 this. I am using the second while loop you have listed because it looks much cleaner. Thank you for your help! – Kelly Lavertu Mar 08 '13 at 04:45
  • @KellyLavertu: Even if you don't have the rep to upvote it, you can accept the answer by clicking on the tick symbol on the left, near the votes. – Jon Skeet Mar 08 '13 at 04:51
  • @JonSkeet: Thanks for telling me that..I accepted your answer because you pushed me in the right direction without spoiling too much for me. I appreciate your time and help! ^_^ – Kelly Lavertu Mar 08 '13 at 05:33