Hi all ..
I've been doing my coursework ..
I have done 30% of it ..
but I've met problem since the day before yesterday ..
I've tried to solve it but I couldn't ..

I've been requested to write programme that promote the user to enter a file name then it will be read ..

the problem that I've got is when the file is being read then copied to a String I always get ?? ( two question mark s) just in the index [0][0] after printing it .. !!
but all the other outputs are correct ..

NOTE: I've not write the Exceptions yet .. I will do when every thing work ..
this is my code ..

import java.io.*;

class StudentMarks{
	public static void main(String[] args) throws Exception {
		// local variables 
		String FileName, Read, ID, M, Marks;
		String [][] str1 = new String [500][3]; 

		
		System.out.println("Enter the File name: ");
		FileName = UserInput.readString();
		
		File fis = new File(FileName);
		BufferedReader file = new BufferedReader(new FileReader(fis)); 
	
		for (int x = 0; (Read =file.readLine())!= null ; x++) {
			// the ID, M and Marks will be initialized to null
			ID = null;  
			M = null; 
			Marks = null; 
			String[] str2 = replace.split(","); // here str2 contains {ID, M, Marks}
			
			ID = str2[0];		// ID will be set in index 0 in str2
			M = str2[1];		// M will be set in index 1 in str2
			Marks = str2[2];	// Marks will be set in index 2 in str2
				
			str1[x][0] = ID;	 // ID will always be set in index[x][0] in str1
			str1[x][1] = M;		 // M will always be set in index [x][1] in str1
			str1[x][2] = Marks;  // Marks will always be set in index [x][2]in str1
			} // end of for 
		
		file.close();
		System.out.println(str1[0][0]); 
		System.out.println("----");
	} // end of main
} // end of class

the output that I get is

Enter the File name:
marks.csv
??acc09u
----

I don't know why questions marks .. !! there is no questions marks in marks.csv file ..

I also try to use

str[0][0].equals(str[1][0])

to compare between 2 variables in the dimensional arrays .. it's compiled correctly but the output is wrong .. !!

could you please give me an explanation before the answer so I can understand the reason of these 2 problems and understand what I'm doing because I have to write report about that .. ^_^

Thanks in advance .. =)

NOTE: I've not write the Exceptions yet .. I will do when every thing work ..

There may be a more stupid thing posted this year on Daniweb, but I doubt it. How can you possibly try to debug your code when you chose to ignore any and all arrors detected by the Java runtime?
Put in a simple try/catch and do a printStackTrace on any Exception that's caught. You may be surprised at what you find, and even if there are no Exceptions that will automatically eliminate the vast majority of possible mistakes.

... on further thought that was not the most stupid thing, although it is a close relative. This is the most stupid...

public static void main(String[] args)  {
   try {
      // all the program code goes here
   } catch(Exception e) {
   }
}

don't judge quickly .. !! because I've tried to do that and I've got the same problem .. !! ^_^
I know this code must be written inside method .. !!!

String filename;
		String str;
		
		System.out.print(" Please Enter file name:");
		filename = UserInput.readString();
		
		BufferedReader inputStream;
		try {
			inputStream = new BufferedReader(new FileReader(filename));
			
			while ((str = inputStream.readLine()) != null) {
				process(str);
			}
			inputStream.close();
		} catch (IOException e) {
			System.out.println(" Error Reading file. " + e.getMessage());
			e.printStackTrace();
		}

it's not stupid to learn and try to solve something .. :)
and it's not shame to make mistake and learn from it ..
Thanks again for replaying .. !! !!

Edited 6 Years Ago by M.S.A : n/a

OK, I wanted to make a point, maybe I made it too strongly!
Anyway, line 21 you split a variable called replace. Where did that come from?
In what way does the output differ from the expected value of the first ID (you haven't shown us the data)?
Your String comparison syntax is sensible and correct. In what way is it nor giving you the expected result?
Finally, to help with your debugging, use lots of System.out.println statements to show the intermediate reults as the program executes. You can comment them out when it's working. Eg: print the input line as soon as you have read it. Priont the ID etc as soon as they have been parsed.

Hi Mr.JamesCherrill ..
line 21: I forgot to delete this .. it should be Read.split(""); I corrected in my computer it but I forgot to correct it here ..

have a look at the last modifications that I've made

import java.io.*;

class F{
	public static void main(String[] args){
		
		String FileName, Read,ID, M, Marks;
		String [][] str1 = new String [500][3]; 
		File fis;
		BufferedReader file;
		
		System.out.println("Enter the File name: ");
		FileName = UserInput.readString();
		
		try {
			fis = new File(FileName);
			file = new BufferedReader(new FileReader(fis));
			
			for (int x = 0; (Read =file.readLine())!= null ; x++) {
				
				ID = null;
				M = null; 
				Marks = null; 
				String[] str2 = Read.split(","); // here str2 contains {ID, M, Marks}
				
				ID = str2[0];	//the ID will be set in  index 0 in str2
				M = str2[1];	//the M will be set in index 1 in str2
				Marks = str2[2]; //the Marks will be set in index 2 in str2
				
				str1[x][0] = ID;
				str1[x][1] = M;
				str1[x][2] = Marks;
				
				System.out.println(str1[x][0] + " --- " + str1[x][1] + " --- " + str1[x][2]);
				System.out.println("======");
				
			} // end of for 
			file.close();
		} // end of try 
		
		catch( FileNotFoundException e )
		{
			System.out.println( " The file does not exist" + e.getMessage() );
		} // end of 1st catch
		
		catch(Exception e) {
			System.out.println(" Error Reading file. " + e.getMessage());
			e.printStackTrace();
		} // end of 2nd catch
		
		
		// the comparison
		System.out.print("Enter ID1: ");
		String ID1 = UserInput.readString(); // I entred ID1 = dpf09u
		System.out.println("So now the str1[7][0] is " + str1[7][0]+" and ID1 is "+ ID1);
		
		if (str1[7][0].equals(ID1)) {
			System.out.println("true");
		} // end of if 
		else {
			System.out.println("this ID doesn't exist ");
			
		} // end of else 
		
	} // end of main
} // end of class

and the outputs are

Enter the File name: 
me.text
?? acc09u --- G51APS --- 70
======
 acc09u  --- G50ALG --- 29
======
 axh16u  --- G50ALG --- 54
======
 axh16u  --- G50PRO --- 61
======
 dkl09u  --- G50PRO --- 24
======
 dpf09u  --- G51APS --- 59
======
 dpf09u  --- G50ALG --- 51
======
 dpf09u  --- G50PRO --- 60
======
 dpk09u  --- G51APS --- 75
======
 dpk09u  --- G50ALG --- 96
======
 dpk09u  --- G50PRO --- 84
======
 drl09u  --- G51APS --- 95
======
 drl09u  --- G50ALG --- 60
======
 drl09u  --- G50PRO --- 36
======
 dxj09u  --- G51APS --- 29
======
 dxj09u  --- G50ALG --- 28
======
 dxj09u  --- G50PRO --- 25
======
 dxk09u  --- G51APS --- 25
======
 dxk09u  --- G50ALG --- 31
======
 Error Reading file. 1
java.lang.ArrayIndexOutOfBoundsException: 1
	at F.main(F.java:26)
Enter ID1: dpf09u
So now the str1[7][0] is  dpf09u  and ID1 is dpf09u

 this ID doesn't exist

If str2[1] doesn't exist on like 26 that means the parsing of the data didn't come up with enough delimiters. Maybe you have an extra carriage return (empty line) at the end of the data file? Try ading a quick print of Read at line 17 to see what the data looks like when read before parsing it.
Also, it looks like you may have leading or trailing blanks on the fields after they are parsed (once agin, I don't have the data file). This could explain the failed equals test. A quick trim() will get rid of them if necessary.
ps: Java proramming convention is: variables names begin with a lower case letter (class names with an upper case letter).

Edited 6 Years Ago by JamesCherrill: n/a

I'm new to java ..
I've written this to print line 16 and 17 .. but I'm not sure is it right way or not ..

int i = 0;
	for ( i = 0 ; i <18; i ++) {
		Read =file.readLine();
		if ( i == 16) {
			System.out.println(Read);
		}end of if
			
		if ( i == 17) {
			System.out.println(Read);
		} end of if
	} // end of for

and the outputs are ..

dxj09u ,G50PRO,25
 dxk09u ,G51APS,25

Sorry, I meant line 17 of your code That way it will print every line just as you read it.
Anyway, it looks like leading blanks on line 17, which will screw up your equals test. Use trim() (method in String class) on all your individual data items after splitting them, but before storing them in your 2D array

yeah I did it again and I printed the last line and the line after it which was null
the result was

Enter the File name: 
me.text

null

there was a space ..

after removing the space from the file I did it again with the last line and the line after it which was null ..

Enter the File name: 
me.text
 dxk09u ,G50ALG,31
null

Sorry man, have to go out now. Trim the blanks like I said. Don't know where the null is coming from - your loop shoiuld prevent that. I'll check this thread again tomorrow.

after removing the space line .. and adding trim()

String[] str2 = Read.split(","); // here str2 contains {ID, M, Marks}
				
	str2 [0] = str2[0].trim();
	str2 [1] = str2[1].trim();
	str2 [2] = str2[2].trim();
				
	ID = str2[0];	
	M = str2[1];	
	Marks = str2[2]; 
							
	str1[x][0] = ID;
	str1[x][1] = M;
	str1[x][2] = Marks;

the questions marks hasn't disappeared ..
the error message has disappeared ..
the outputs are:

Enter the File name: 
me.text
?? acc09u --- G51APS --- 70
======
acc09u --- G50ALG --- 29
======
axh16u --- G50ALG --- 54
======
axh16u --- G50PRO --- 61
======
dkl09u --- G50PRO --- 24
======
dpf09u --- G51APS --- 59
======
dpf09u --- G50ALG --- 51
======
dpf09u --- G50PRO --- 60
======
dpk09u --- G51APS --- 75
======
dpk09u --- G50ALG --- 96
======
dpk09u --- G50PRO --- 84
======
drl09u --- G51APS --- 95
======
drl09u --- G50ALG --- 60
======
drl09u --- G50PRO --- 36
======
dxj09u --- G51APS --- 29
======
dxj09u --- G50ALG --- 28
======
dxj09u --- G50PRO --- 25
======
dxk09u --- G51APS --- 25
======
dxk09u --- G50ALG --- 31
======
Enter ID1: dpf09u
So now the str1[z][0] is dpf09u/ and ID1 is dpf09u

 this ID doesn't exist

OK Mr.JamesCherrill .. Thanks for helping ..
I'll try to solve that till you back tomorrow ..
have a great night .. ^_^

Maybe you can try this to read your file

File fil = new File(FileName);
FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(fil);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      //Then you can write your code
      fis.close();
      bis.close();
      dis.close();
}catch(Exception e){e.printStackTrace();}

Or you can try this to read

FileInputStream fstream = null;
        try
        {
            File fil = new File(FileName);
            fstream = new FileInputStream(fil);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader out1 = new BufferedReader(new InputStreamReader(in));

//Write your code

out1.close();
in.close();

}catch (Exception ex) {ex.printStackTrace();}

And check if still there is a problem of ?? in ouput

Hi gunjannigam ..
Unfortunately .. the problem is still there ..
I don't know why it's just with the 1st line .. !!

BTW .. is there any difference of using java in Macintosh or Windows .. ?
because I use Macintosh and I've taken my lessons in Windows .. and I don't know if there is any difference ..

Thanks ..

I would try just deleting the first line from the data file and see if the ?? go away.
Java on mac/win/linux is all the same.

I did that by deleting the 1st line, 2nd line ..... till 5th line .. but the results were still the same ..

I'm wounding if there is wrong in the way of reading file in my codes .. !!
and yeah I printed the result as the file is being read but the ?? were in the result .. :S

yeah I forgot to say that .. I changed the beginning of the text from these datas ( acc09u --- G50ALG --- 29) to just numbers .. the ?? didn't appear .. !!

This article has been dead for over six months. Start a new discussion instead.