Hi I am very new to java as its part of my course and I am stuck
I have written part of this basic code below

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.imageIO;
import.java.awt.Color;
public class FinalExam
{
	public static void main(String [] args) throws IOException
	{
		Scanner keyboardInput = new Scanner(System.in);
		System.out.println("Input the address of the image (e.g. c:\\myPicture.jpg)");
		String imageLocation = keyboardInput.nextLine();
		File imageFile = new File(imageLocation);
		BufferedImage storedImage = ImagIO.read(imageFile);
	}
}

does anyone know I would change this code so that you would be allowed to supply the name of an image file a a command line argument when issuing the command to run the program?

Recommended Answers

All 6 Replies

You could use the args array you have, and have the imageLocation = args[0] where args[0] is the string entered right after the command to execute the program:

java FinalExam imageLocationhere

Alright, whoever voted that down be man enough and stand up to admit to it (which is probably the reason (s)he didn't use the "rep" link, as that transmits the userId along with it).

Who was that didn't like the fact that it was pointed out the user crossposted, without being respectful enough to refer to the original post, so that anyone answering here would more than likely simply been wasting their time by making suggestions that have already benn made, as happened above?

In that case, I will ask you if also pay bills your partner has already paid? Do you sweep the floor 5 minutes after someone else has already done it? Etc, etc. All of these are a waste of time and/or money (which a waste of time is a waste of money, even if not always yours), including the cross-posting.

So, who took offense at the fact a cross-post was pointed out (in an attempt to save others from wasting their time), but didn't take offense at the cross-post itself?

And, for the record, I have nothing against cross-posting to another site, as long as the OP makes reference to the other post so that any one responding can see what's already been suggested rather than wasting their time repeating things.

Edit: Then again, it was probably the OP that voted it down, since the repeat answer is already voted down and the truth is simply too much to bear (i.e. (s)he was not provided with a completely finished program to block copy cut-n-paste).

Alright, whoever voted that down be man enough and stand up to admit to it (which is probably the reason (s)he didn't use the "rep" link, as that transmits the userId along with it).

Who was that didn't like the fact that it was pointed out the user crossposted, without being respectful enough to refer to the original post, so that anyone answering here would more than likely simply been wasting their time by making suggestions that have already benn made, as happened above?

In that case, I will ask you if also pay bills your partner has already paid? Do you sweep the floor 5 minutes after someone else has already done it? Etc, etc. All of these are a waste of time and/or money (which a waste of time is a waste of money, even if not always yours), including the cross-posting.

So, who took offense at the fact a cross-post was pointed out (in an attempt to save others from wasting their time), but didn't take offense at the cross-post itself?

And, for the record, I have nothing against cross-posting to another site, as long as the OP makes reference to the other post so that any one responding can see what's already been suggested rather than wasting their time repeating things.

Edit: Then again, it was probably the OP that voted it down, since the repeat answer is already voted down and the truth is simply too much to bear (i.e. (s)he was not provided with a completely finished program to block copy cut-n-paste).

You are right. I mean mellowmike provided correct solution and instead of getting positive rep, he was down voted as well.
I might get down voted too for replying to this thread, but as stated before to another thread, I am not very keen on this voting system without posting some comments explaining the reasons.
It is fair that whoever is down voted, should know the reason and by whom.
Of course this is not the place to discuss this; there are other threads and forums.
I am simply agreeing with you.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.imageIO;
import.java.awt.Color;
public class FinalExam
{
	public static void main(String [] args) throws IOException
	{
		String Xfile=args[0];
		if(Xfile.equals(""))
			System.out.println("Enter a valid file!");
		else
		{
			File imageFile = new File(Xfile);
			BufferedImage storedImage = ImagIO.read(imageFile);
		}
	}
}

Thank you,
Kanishka Dilshan.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.imageIO;
import.java.awt.Color;
public class FinalExam
{
	public static void main(String [] args) throws IOException
	{
		String Xfile=args[0];
		if(Xfile.equals(""))
			System.out.println("Enter a valid file!");
		else
		{
			File imageFile = new File(Xfile);
			BufferedImage storedImage = ImagIO.read(imageFile);
		}
	}
}

Thank you,
Kanishka Dilshan.

Totally wrong. Shows a lack of understanding of arguments. And if you complain that you are not very good with java, why do you give wrong suggestions and code.

This will never run:

String Xfile=args[0];
		if(Xfile.equals(""))
			System.out.println("Enter a valid file!");

the way you think. You will get an ArrayIndexOutOfBoundsException.

I will give you some time to figure it out on your own before posting again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.