Hi, I'm a beginner with Java and I've encountered some trouble with simple I/O. Thing is, I have to enable the command line and write a file name of a textfile which holds a simple number. That number I have to get from the file and into JGrasp and print it out.
The problem occurs when I print it out and the number is 49 or -1 and not 15 which is the number that's actually in the file. I've experimented with the debugger feature and it seems that it finds the number somewhere after looking in the text file. This is really confusing for me, and any help would be really nice.

import java.io.*;

public class HippityHop {

	public static void main(String [] args) {
	try{
	
		String var = args[0];
		FileReader in = new FileReader(var);
		int chr = in.read();
		while(chr != -1) {
		
			chr = in.read();
		}
		
		
		System.out.println(chr);
			
		in.close();		
		
			
	}//end of try
	
		catch(FileNotFoundException fnfe){
		fnfe.printStackTrace();
		
		}
		catch(IOException ioe) {
		ioe.printStackTrace();
		
		}

	}//end main
	
}//end class

Was the data written to the file as a string or as an integer?
...meaning: if you read it as a string, then convert it to an integer, does it work?

Edited 4 Years Ago by thines01: clarity

Your code reads the binary(ASCII) value of the character as an int. If you want to see the int as a character you need to cast it to a char.
'1' has a value of 49
Try this:
System.out.println("49=" + (char)49);

Was the data written to the file as a string or as an integer?
...meaning: if you read it as a string, then convert it to an integer, does it work?

I believe the data is a string because all I did was create a text file with a number in it. But I don't know how to do the conversion, I mean I looked parsing up but I can't figure out how to do it, what do I have to change...it's all really confusing.

The contents of the file is ASCII characters. When you read the bytes from the file as bytes, you get the binary values of the characters: '1' = 49. If you want to read the contents of the file as characters/Strings, you should use a different class and method.
The Scanner class will read data into a String with one of its next....() methods.

while(chr != -1) {
     chr = in.read();
}
System.out.println(chr);

you print "chr" after the while which only exits when chr is equal to -1 so the println should always print -1

I think you may be confusing two concepts. if all you want to do is read in the file name and print it to the console then you would use

import java.io.*;

public class HippityHop {

	public static void main(String [] args) {
	try{
	
		String var = args[0];
		FileReader in = new FileReader(var);
		String chr = in.read();
		System.out.println(chr);
		in.close();		
		
			
	}//end of try
	
		catch(FileNotFoundException fnfe){
		fnfe.printStackTrace();
		
		}
		catch(IOException ioe) {
		ioe.printStackTrace();
		
		}

	}//end main
	
}//end class

there is no need for the 'while' statement because we are not waiting for any name to be input the name is gotten using the command line arguments parsed to the class. also the int chr is not necessary because in.read() method returns a string which is fine for our purpose of printing file name to console.

however if your purpose is to wait for user input and/or get the file name as an integer then one could use

int filenameasint=Integer.parseInt(chr)//chr is string name of file that is a valid integer

hope that helps

Edited 4 Years Ago by David Kroukamp: n/a

Comments
Incorrect info, adds to OP's confusion

@David Kroukamp
Did you compile and test your code?
Why bother posting untested (JUNK) code?

... in.read() method returns a string ...

Absolutely wrong.
You are advising a beginner here, so it's essential that you get your facts right. It's unacceptable to create this kind of confusion. You should check your code and your facts before posting.
You should also read the previous entries - the OP's problem was confusion between the numeric value of a char and the letter it represents. By confusing int with String you are just making things a lot worse.

Edited 4 Years Ago by JamesCherrill: n/a

Comments
Better, you thought about it, I reacted w/o thought
This article has been dead for over six months. Start a new discussion instead.