Nieodpowiednie generowanie znaków i liter do haseł.

0

Witam. Otóż napisałem prosty programik który miał generować hasła. Niestety zamiast wyświetlać całych haseł wyświetla tylko jedną literę. A poza tym nie chce generować znaków bo wywala jakiś błąd. Może ktoś wie gdzie jest błąd ?!

import java.util.*;

public class Main {
	
	static Random rand = new Random();
	
	static char alfabet_small[] = {'a','b','c','d','e','f','g','h','i','j','k','l',
							 	   'm','n','o','p','r','s','t','u','w','x','y','z'};
	static char alfabet_big[] =   {'A','B','C','D','E','F','G','H','I','J','K','L',
								   'M','N','O','P','R','S','T','U','W','X','Y','Z'};
	static char chars[] = {'!','@','#','$','%','^','&','*','<','>','?','Q'};	
	
	static int al_s;
	static int al_b;
	static int ch;
	
	static int max_char;
	
	static char pass;
	
	public static void rand_al_s() {
		for(int i = 0; i < al_s; i++){
			int r;
			r = rand.nextInt((24)+1);
			pass = alfabet_small[r];
		}
	}
	
	public static void rand_al_b() {
		for(int i = 0; i < al_b; i++){
			int r;
			r = rand.nextInt((24)+1);
			pass = alfabet_big[r];
		}
	}
	
	/*public static void rand_ch() {
		for(int i = 0; i < ch; i++){
			int r;
			r = rand.nextInt((12)+1);
			pass = chars[r];
		}
	}*/
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Podaj dlugosc hasla");
		max_char = input.nextInt();
		
		al_s = rand.nextInt((max_char)+1);
		al_b = rand.nextInt((max_char - al_s)+1);
		ch = max_char-(al_s+al_b);
		
			rand_al_s();
			rand_al_b();
			//rand_ch();
		
		
		System.out.println("Twoje haslo to:   " + pass);
	}

}

A, i jeszcze jedno pytanie czy lepiej użyć klasy math czy rand.

0

Wyświetla tylko jedną literę, bo zmienna pass nie może przechować ich więcej, zresztą nigdzie nie próbujesz przechować całego hasła.

0

A, dzięki poprawiłem ale teraz wyświetla się błąd przy:

pass = alfabet_small[r];
pass = alfabet_big[r];

Poprawiony kod:

import java.util.*;

public class Main {
	
	static Random rand = new Random();
	
	static char[] alfabet_small = {'a','b','c','d','e','f','g','h','i','j','k','l',
							 	   'm','n','o','p','r','s','t','u','w','x','y','z'};
	static char[] alfabet_big =   {'A','B','C','D','E','F','G','H','I','J','K','L',
								   'M','N','O','P','R','S','T','U','W','X','Y','Z'};
	static char[] chars = {'!','@','#','$','%','^','&','*','<','>','?','Q'};	
	
	static int al_s;
	static int al_b;
	static int ch;
	
	static int max_char;
	
	static char pass[];
	
	public static void rand_al_s() {
		for(int i = 0; i < al_s; i++){
			int r;
			r = rand.nextInt((24)+1);
			pass = alfabet_small[r];
		}
	}
	
	public static void rand_al_b() {
		for(int i = 0; i < al_b; i++){
			int r;
			r = rand.nextInt((24)+1);
			pass = alfabet_big[r];
		}
	}
	
	/*public static void rand_ch() {
		for(int i = 0; i < ch; i++){
			int r;
			r = rand.nextInt((12)+1);
			pass = chars[r];
		}
	}*/
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Podaj dlugosc hasla");
		max_char = input.nextInt();
		
		al_s = rand.nextInt((max_char)+1);
		al_b = rand.nextInt((max_char - al_s)+1);
		ch = max_char-(al_s+al_b);
		
			rand_al_s();
			rand_al_b();
			//rand_ch();
		
		
		System.out.println("Twoje haslo to:   " + pass);
	}

}
0

Moim zdaniem problem jest tutaj: r = rand.nextInt((12)+1);
Tablica chars ma 12 znakow a ty losujesz od 1 do 13 ( to 12 oznacza ILE ponad ten 1 ).

0

To nie jest błąd bo przy generowaniu znaku to 12 zamienia się w 11 więc trzeba dodać 1.

Kod został ale jest problem z konwersją tablicy znaków na string.

import java.util.*;

public class Main2 {
	
	static Random rand = new Random();
	
	static char[] chars = {'A','B','C','a','b','!','@','c','d','G','H','I','e','f','g',
						   'h','i','j','k','l','N','O','P','M','R','X','Y','Z','>','?',
						   'Q','#','$','%','m','n','o','D','E','p','S','T','^','&','*',
						   '<','U','W','r','s','t','u','w','x','F','J','K','L','y','z'};
	
	static int max_char;
	
	static char pass;
	static char pa_ss[];
	
	static int rand_char;
	
	static int t;
	static char c;
	
	public static void rand_ch() {

			rand_char = rand.nextInt((60)+1);
			pass = chars[rand_char];
	}
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Podaj dlugosc hasla");
		max_char = input.nextInt();
		
		for(int i = 0; i > max_char; i++){
			rand_ch();
			pa_ss[i] += pass;
			rand_char = t;
			pass = c;
		}
		
		String _pass = new String(pa_ss);
		
		System.out.println("Twoje haslo to:   " + _pass);
	}

}

Błędy przy kompilacji:

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.<init>(Unknown Source)
	at Main.main(Main.java:41)
0

To nie jest błąd kompilacji, to jest błąd wykonania.
Deklarujesz tablicę

static char pa_ss[];

ale nigdzie jej nie tworzysz => tablica pa_ss jest nullem. W tym wierszu for(int i = 0; i > max_char; i++)

źle napisałeś nierówność, Gdybyś napisał dobrze, to błąd byłby już tu<code class="java">pa_ss[i] += pass;
0

To mógłbyś mi powiedzieć jak to zrobić ?

Poprawiłem

pa_ss[i] += pass;

na

pa_ss[i] = pass;

ale dalej jest ten sam błąd.

Kiedy spróbowałem nadać mu początkową wartość

static char pa_ss[] = {'0'}; 

to pominął wszystkie procedury i wyświetlił, że hasło to 0.

0

Stwórz tę tablicę

pa_ss = new char[max_char];
0

Ok spróbuje w ten sposób bo jak na początku definiowałem tak to nic się nie wyświetlało.

@Edit:

Nic się nie wyświetla.

0

A u mnie w eclpsie nic nie wyskakiwało.

Ale już jest dobrze. Dzięki.

1 użytkowników online, w tym zalogowanych: 0, gości: 1