Hello. I'm attempting to start a Caesar cipher program and can't get the program to display the output key[1]. Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Caesar {
	private String[] key = new String[26];
	
	public static void main(String[] args){
		Scanner kbd = new Scanner(System.in);
		String task = kbd.next();
		int N = kbd.nextInt();
		String file = kbd.next();

		if (!task.equalsIgnoreCase("-d") && !task.equalsIgnoreCase("-e") ){
			System.out.println("usage: -d|-e");
			System.exit(0);
		}
		

        }
		
	public void Decrypt(File file){
		Scanner kscan = null;
			try {
				kscan = new Scanner(file);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		kscan.useDelimiter("\\s");
		for(int i=0; i < key.length; i++){
			key[i] = kscan.next();
		}
		
		System.out.print(key[1]);
	}
}

Any help is much appreciated.

Here is my new code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Caesar {


	public static void main(String[] args){
		Scanner kbd = new Scanner(System.in);
		String task = kbd.next();
		int N = kbd.nextInt();
		String file = kbd.next();

		if (!task.equalsIgnoreCase("-d") && !task.equalsIgnoreCase("-e") ){
			System.out.println("usage: -d|-e");
			System.exit(0);
		}

		if (task.equalsIgnoreCase("-d")){
			Caesar myCaesar = new Caesar(new File(file));
			Scanner dscan = null;
			try{
				dscan = new Scanner(new File(args[1]));
			}
			catch(FileNotFoundException e){
				System.out.println("No such file");
				System.exit(0);
			}
			while (dscan.hasNextLine()){
				System.out.println(myCaesar.decrypt(dscan.nextLine()));
			}
		}
	}

	private String[] key = new String[26];
	public Caesar(File file){
		Scanner kscan = null;
		try{
			kscan = new Scanner(file);
		}
		catch(FileNotFoundException e){
			System.out.println("No such file");
			System.exit(0);
		}
		kscan.useDelimiter("\\s");
		for(int i=0; i < key.length; i++){
			key[i] = kscan.next();
		}

	}
	private String decrypt(String encrypted){
		Scanner kbd = new Scanner(System.in);
		int N = kbd.nextInt();
		
		
		String rval = "";
		for (int i = 0; i< encrypted.length(); i++){
			char letter = encrypted.charAt(i);
			int pos = 0;
			for(int j = 0; j<key.length; j++)
				if(key[j].equalsIgnoreCase(""+ letter)){
					pos = j;
					break;
				}
			if (96<letter && letter < 123)
				rval += (char)(pos + 'a');
			else if (64 < letter && letter < 91)
				rval += (char)(pos + 'a');
			else rval += letter;

		}
		return rval;
	}
}

Some of it is incorrect because I borrowed it from another decrypt program we wrote. I am having the most trouble getting the thing to read the file "file" because I will input datafile.txt which is in the same directory, but it says no file found.

On line 36, you have not identified the actual file name, eg

file = new File(file.txt);

To check that the destination is correct you can use the below methods:

isReadable(Path), isWritable(Path), and isExecutable(Path).

As shown on the Java tutorial ()

"The following code snippet verifies that a particular file exists and that the program has the ability to execute the file."

Path file = ...;
boolean isRegularExecutableFile = Files.isRegularFile(file) &
                                  Files.isReadable(file) &
                                  Files.isExecutable(file);

Line 36 is a one parameter constructor
public Caesar(File file){...
so it's right not to code an actual file name. The actual file name comes from lines 12 and 20 in main.
isReadable is a useful call here, but there's no requirement for it to be writable, and its highly unlikely to be executable.

To find out exactly what and where Java is looking for as your file, print file.getAbsolutePath(). It's not unusual for it to be looking ina dircteory you didn't expect, and this will make it clear.

Edited 5 Years Ago by JamesCherrill: fixed: getAbsolurtePath, not getPath

Thanks for the help guys! Ok, so this is my new code:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;



public class Caesar {



	String[] key = new String[26];

	public Caesar(File file){



		Scanner kscan = null;



			try {

				kscan = new Scanner(file);

			} catch (FileNotFoundException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

		

		

		kscan.useDelimiter("\\s");

		for(int i=0; i < key.length; i++){

			key[i] = kscan.next();

		}



	}

	

	

	public static void main(String[] args){

		Scanner kbd = new Scanner(System.in);

		String task = kbd.next();

		int N = kbd.nextInt();

		String filename = kbd.next();

		File file = new File(filename);



		if (task.equalsIgnoreCase("-d")){

			Caesar myDecrypt = new Caesar(file);

			Scanner dscan = null;

				dscan = new Scanner(file);



			while (dscan.hasNextLine()){

				System.out.println(myCaesar.decrypt(dscan.nextLine()));

			}

		}

		

		else if (task.equalsIgnoreCase("-e")){

			Caesar myCaesar = new Caesar(file);

			Scanner dscan = null;

				dscan = new Scanner(file);



			while (dscan.hasNextLine()){

				System.out.println(myCaesar.encrypt(dscan.nextLine()));

			}

			



			

		}

		else System.out.println("usage: -d|-e");

		System.exit(0);

	}





	private String decrypt(String encrypted){

		Scanner kbd = new Scanner(System.in);

		int N = kbd.nextInt();

		

		

		String rval = "";

		for (int i = 0; i< encrypted.length(); i++){

			char letter = encrypted.charAt(i);

			int pos = 0;

			for(int j = 0; j<key.length; j++)

				if(key[j].equalsIgnoreCase(""+ letter)){

					pos = j;

					break;

				}

			if (96<letter && letter < 123)

				rval += (char)(pos + N);

			else if (64 < letter && letter < 91)

				rval += (char)(pos + N);

			else rval += letter;



		}

		return rval;

	}

	

	private String encrypt(String decrypted){

		Scanner kbd = new Scanner(System.in);

		int N = kbd.nextInt();

		

		

		String rval = "";

		for (int i = 0; i< decrypted.length(); i++){

			char letter = decrypted.charAt(i);

			int pos = 0;

			for(int j = 0; j<key.length; j++)

				if(key[j].equalsIgnoreCase(""+ letter)){

					pos = j;

					break;

				}

			if (96<letter && letter < 123)

				rval += (char)(pos - N);

			else if (64 < letter && letter < 91)

				rval += (char)(pos - N);

			else rval += letter;



		}

		return rval;

}

}

And it won't run or give me any kind of error message. Any suggestions?

Put in some print statements so you can see exactly what is or isn't being executed.
Start with one on line 56 to confirm that main is being called, and go on from there.

Put a println just after the main method header about line 55.

I can't even print

If you get error messages please post the message here.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Caesar {

	String[] key = new String[26];
	public Caesar(File file){
		Scanner kscan = null;
		try{
			kscan = new Scanner(file);
		}
		catch(FileNotFoundException e){
			System.out.println("No such file");
			System.exit(0);
		}
		kscan.useDelimiter("\\s");
		for(int i=0; i < key.length; i++){
			key[i] = kscan.next();
		}

	}
	
	public static void main(String[] args) throws FileNotFoundException{
		Scanner kbd = new Scanner(System.in);
		String task = kbd.next();
		int N = kbd.nextInt();
		String filename = kbd.next();
		File file = new File(filename);
		Scanner filescanner = new Scanner(file);
		String words = filescanner.nextLine();
		

		if (task.equalsIgnoreCase("-d")){
			Caesar myCaesar = new Caesar(file);
		
				

			while (filescanner.hasNextLine()){
				System.out.println(myCaesar.decrypt(filescanner.nextLine()));
			}
		}
		
		else if (task.equalsIgnoreCase("-e")){
			Caesar myCaesar = new Caesar(file);
			

			while (filescanner.hasNextLine()){
				System.out.println(myCaesar.encrypt(filescanner.nextLine()));
			}
			

			
		}
		else System.out.println("usage: -d|-e");
		System.exit(0);
	}



	private String decrypt(String encrypted){
		Scanner kbd = new Scanner(System.in);
		int N = kbd.nextInt();
		
		
		String rval = "";
		for (int i = 0; i< encrypted.length(); i++){
			char letter = encrypted.charAt(i);
			int pos = 0;
			for(int j = 0; j<key.length; j++)
				if(key[j].equalsIgnoreCase(""+ letter)){
					pos = j;
					break;
				}
			if (96<letter && letter < 123)
				rval += ((char)(pos + 'a'));
			else if (64 < letter && letter < 91)
				rval += ((char)(pos + 'a'));
			else rval += letter;

		}
		return rval;
	}
	
	private String encrypt(String decrypted){
		Scanner kbd = new Scanner(System.in);
		int N = kbd.nextInt();
		
		
		String rval = "";
		for (int i = 0; i< decrypted.length(); i++){
			char letter = decrypted.charAt(i);
			int pos = 0;
			for(int j = 0; j<key.length; j++)
				if(key[j].equalsIgnoreCase(""+ letter)){
					pos = j;
					break;
				}
			if (96<letter && letter < 123)
				rval += ((char)(pos - N)%26);
			else if (64 < letter && letter < 91)
				rval += ((char)(pos - N)%26);
			else rval += letter;

		}
		return rval;
}
}

There is my modified code with the following error messages:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:855)
at java.util.Scanner.next(Scanner.java:1364)
at Caesar.<init>(Caesar.java:19)
at Caesar.main(Caesar.java:35)

Your code is calling the Scanner class's next() method on line 19. The method is throwing an error.
Have you read the API doc for the Scanner class's next() method to see what that error means?

While you are reading the API doc you should also look at the has... methods to see if any of them would be useful in preventing this error.

Edited 5 Years Ago by NormR1: n/a

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