2

Hello i've a problem with my java program the caesar cipher: it gives me this errors:

CifrarioCesare.java:27: error: non-static method cripta(String,int) cannot be referenced from a static context
            c = cripta(s, spost);
                ^
CifrarioCesare.java:31: error: non-static method decripta(String,int) cannot be referenced from a static context
            c = decripta(s, spost);
                ^
2 errors

This is the code

import java.util.*;
/*
Cifrario di Cesare
*/
public class CifrarioCesare
{
public static void main(String[] args)
{
    String c;
    System.out.println(" ");
    System.out.println("\t#################################");
    System.out.println("\t####### Cifrario Di Caesar ######");
    System.out.println("\t### ........................ ###");
    System.out.println("\t############# Crypt #############");
    System.out.println("\t#################################");
    System.out.println(" ");
    Scanner t = new Scanner(System.in);
    System.out.println("> Desideri codificare o decodificare un messaggio?\n(1) Codifica\n(2) Decodifica");
    int code = t.nextInt();
    System.out.println("> Scrivi la stringa  da codificare");
    String s = t.nextLine();
    s = s.replaceAll(" ","");
    System.out.print("> Scrivi la chiave numerica intera: ");
    int spost = t.nextInt();
    if (code==1)
    {
        c = cripta(s, spost);
    }
    else if (code==2)
    {
        c = decripta(s, spost);
    }
    else
    {
        System.exit(0);
    }
    System.out.print("> Stringa criptata:\n\t" + c + "\n\n");
}

/* Cript */

public String cripta(String s, int spost) 
{
    String alfabeto = "abcdefghijklmnopqrstuvwxyz";
    String stringa = "";
    // ex: s = "ciao" -> j=4
    int j = s.length();
    int index;
    char y;
    // per i che va da zero a 4 (non compreso)...
    for (int i=0; i<j; i++)
    { // inizia ciclo for
        // se i=0, y="c".. i=1, y="i".. i=2, y="a".. i=3, y="o"
        y = s.charAt(i);
        // se y="c", string.indexOf("c") va a cercare dove quel carattere è 
        // situato nell'alfabeto. In questo caso (partendo da a=0, b=1...) 
        // il carattere "c" è uguale a 2, quindi index=2.
        /*  ---------------------
        >>> stessa cosa per le altre lettere <<<
            ---------------------     */
        index = alfabeto.indexOf(y);
        // ora inserisco in y il nuovo carattere "crittografato" perchè
        // aggiungo al carattere iniziale lo spostamento inserito dall'utente
        /* In pratica:
            Se spost=3; (con l'alfabeto semplificato)
            "c"+3="f".."i"+3="n".. ecc..    
        Il problema quel'e'? Che se la ad esempio ho "z"+3 dovrebbe essere "c", 
        ma per far questo, devo impostare un while in modo che 
        se la somma è maggiore di 27 (z=25) -> 25+3=28, e non va bene perchè la stringa alfabeto
        arriva massimo a 25, quindi se è maggiore devo cavare 26 cosicche torni entro i 25 caratteri */
        while ((index+spost)>25)
        {
            index -= 26;
        }
        y = alfabeto.charAt(index+spost);
        // infine inserisco nella nuova stringa volta per volta i nuovi caratteri
        // N.B. a+=b corrisponde ad a=a+b
        stringa += y;
    } // fine ciclo for
    // ora restituisco la stringa finale criptata
    return stringa;
}

/* Decript */

public String decripta(String s, int spost) 
{
    String alfabeto = "abcdefghijklmnopqrstuvwxyz"; 
    String stringa = "";
    // ex: s = "afjgr" -> j=5
    int j = s.length();
    int index;
    char y;
    // per i che va da zero a 5 (non compreso)...
    for (int i=0; i<j; i++)
    { // inizia ciclo for
        y = s.charAt(i);
        index = alfabeto.indexOf(y);
        while ((index-spost)<0)
        {
            index += 26;
        }
        y = alfabeto.charAt(index-spost);
        stringa += y;
    } // fine ciclo for

    // ora restituisco la stringa finale decriptata
    return stringa;
}
}

I've tried to write static in the two functions but i've the main problem. What i've to do? (i don't want to write it in 2 files or using another class, i'd like to do it in a unique file. If it's possible, i'm asking you the how-to. Thanks.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186

1 Answers1

1

In the main method, create an instance of your class and use it for calling your methods.

CifrarioCesare cc = new CifrarioCesare();
if (code==1){
    c = cc.cripta(s, spost);
}else if (code==2){
    c = cc.decripta(s, spost);
}
Kojotak
  • 2,020
  • 2
  • 16
  • 29
  • thanks solved the main problem but now all is running but when i have to read the message to crypt or decrypt it pass over without reading. > Desideri codificare o decodificare un messaggio? (1) Codifica (2) Decodifica 1 > Scrivi la stringa da codificare (HERE -> i cannot write it goes over) > Scrivi la chiave numerica intera: – Tiziano Loreti Oct 21 '13 at 08:41
  • it seems like it doesn't read this line: `String s = t.nextLine();` – Tiziano Loreti Oct 21 '13 at 08:43
  • using "next" instead "nextline" it run but it gives me error later, in the "nextInt"... what i could do? – Tiziano Loreti Oct 21 '13 at 08:50
  • The nextLine() reads the rest of the previous line with the integer. Try to run your program and for the first question, enter "1 test". It will encrypt the test string. To fix this, I would use only nextLine() and parse the c integer from string (or use string comparsion in the if statements). – Kojotak Oct 21 '13 at 08:53