Keep getting Null on String with Random - java

it's Java Starter here, so i was Coding my program which makes a Password, i used the Random Object to have a random Number that will choose what Letter comes or What number comes.
I keep getting the Numbers and the Symbols Only.. what i get is something Like this:
"9=9null=99" I don't understand Why there's a null. Java doesn't show me any problems with my code (Doesn't show Null Pointer exception.)
Here's My code:
package project.Secureword.com;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Random;
import javax.swing.*;
public class Main {
private static JFrame start;
private static TextField field;
private static String password;
private static String lastpass;
public static Settings passsettings;
public static Random r;
public static void main(String[] args) {
passsettings = new Settings();
passsettings.setIfDots(true);
passsettings.setIfLowercase(true);
passsettings.setIfNum(true);
passsettings.setIfUpprcase(true);
Settings.changerun();
start();
}
public static void start() {
Font a = new Font(null, Font.BOLD, 0);
Font size = a.deriveFont(20f);
JButton createPass = new JButton();
createPass.setText("<html> Click me to <br> Generate a Password! <html>");
createPass.setPreferredSize(new Dimension(100,100));
JLabel text = new JLabel();
text.setText("<html> Welcome to SECUREPASS! <br> An Extreme Strong Password Generator <br> Developed by OfficialCode");
text.setPreferredSize(new Dimension(150,150));
text.setFont(size);
field = new TextField(20);
field.setSize(new Dimension(50,50));
start = new JFrame("Secureword | Strong password Generator | Coded by OfficialCode");
start.setPreferredSize(new Dimension(390,390));
start.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
start.setLocationRelativeTo(null);
start.setLayout(new BorderLayout());
start.getContentPane().add(createPass, BorderLayout.WEST);
start.getContentPane().add(text, BorderLayout.PAGE_START);
start.getContentPane().add(field, BorderLayout.CENTER);
start.pack();
start.setVisible(true);
password = "";
createPass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
field.setText(null);
field.selectAll();
allrandom();
}});
}
public static void allrandom() {
String[] pass = new String[8];
Random r = new Random();
for(int i = 0 ; i < pass.length ; i++) {
int ch = 0;
ch = r.nextInt(3);
switch(ch) {
case 1:
if(passsettings.isIfDots()) {
String newchar = "";
newchar = dotpass();
pass[i] = newchar;
break;
}
case 2:
if(passsettings.isIfNum()) {
String newchar2 = "";
newchar2 = Numpass();
pass[i] = newchar2;
break;
}
case 3:
String newchar3 = "";
newchar3 = charchose();
pass[i] = newchar3;
break;
}
}
for(int i = 0 ; i < pass.length ; i++) {
String newpasschar = "";
newpasschar = pass[i];
password = password + newpasschar;
}
field.setText(password);
// random() end
}
public static String uppercasepass() {
Random r = new Random();
String passletter = "";
int rN3 = 0;
rN3 = r.nextInt(26);
switch(rN3) {
case 1:
passletter = "A";
case 2:
passletter = "B";
case 3:
passletter = "C";
case 4:
passletter = "D";
case 5:
passletter = "E";
case 6:
passletter = "F";
case 7:
passletter = "G";
case 8:
passletter = "H";
case 9:
passletter = "I";
case 10:
passletter = "J";
case 11:
passletter = "K";
case 12:
passletter = "L";
case 13:
passletter = "M";
case 14:
passletter = "N";
case 15:
passletter = "O";
case 16:
passletter = "P";
case 17:
passletter = "Q";
case 18:
passletter = "R";
case 19:
passletter = "S";
case 20:
passletter = "T";
case 21:
passletter = "U";
case 22:
passletter = "V";
case 23:
passletter = "W";
case 24:
passletter = "X";
case 25:
passletter = "Y";
case 26:
passletter = "Z";
}
return passletter;
// uppercase() end
}
public static String lowercasepass() {
Random r = new Random();
String passletter = "";
int rN3 = 0;
rN3 = r.nextInt(26);
switch(rN3) {
case 1:
passletter = "a";
case 2:
passletter = "b";
case 3:
passletter = "c";
case 4:
passletter = "d";
case 5:
passletter = "e";
case 6:
passletter = "f";
case 7:
passletter = "g";
case 8:
passletter = "h";
case 9:
passletter = "i";
case 10:
passletter = "j";
case 11:
passletter = "k";
case 12:
passletter = "l";
case 13:
passletter = "m";
case 14:
passletter = "n";
case 15:
passletter = "o";
case 16:
passletter = "p";
case 17:
passletter = "q";
case 18:
passletter = "r";
case 19:
passletter = "s";
case 20:
passletter = "t";
case 21:
passletter = "u";
case 22:
passletter = "v";
case 23:
passletter = "w";
case 24:
passletter = "x";
case 25:
passletter = "y";
case 26:
passletter = "z";
}
return passletter;
}
public static String Numpass() {
Random r = new Random();
String passletter = "";
int rN = 0;
rN = r.nextInt(9);
switch(rN) {
case 1:
passletter = "1";
case 2:
passletter = "2";
case 3:
passletter = "3";
case 4:
passletter = "4";
case 5:
passletter = "5";
case 6:
passletter = "6";
case 7:
passletter = "7";
case 8:
passletter = "8";
case 9:
passletter = "9";
}
return passletter;
}
public static String dotpass() {
Random r = new Random();
String passletter = "";
int rN5 = 0;
rN5 = r.nextInt(5);
switch(rN5) {
case 1:
passletter = "_";
case 2:
passletter = "]";
case 3:
passletter = "$";
case 4:
passletter = "#";
case 5:
passletter = "=";
}
return passletter;
}
public static String charchose() {
Random r = new Random();
String line = "";
int rN = 0;
rN = r.nextInt(20);
switch(rN) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
if(passsettings.isIfLowercase()) {
line = lowercasepass();
}
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
if(passsettings.isIfUpprcase()) {
line = uppercasepass();
}
}
return line;
}
}

The problem comes from the fact that Random.nextInt(n) return a value between 0-n (exclusive). Your switch case are all wrong by one.
This explain your null since you don't do anything for ch = 0, the String stays at null:
switch(ch){
case 1: ... //symbol
case 2 ... //number
case 3: ... //letter
}
This should be
switch(ch){
case 0: ... //symbol
case 1: ... //number
case 2: ... //letter
}
Some other issues or improvments :
You don't break after a case so you always get the last case executed (well all following case). Resulting in a possibilities of 4 character zZ9= (easy to bruteforce !)
You should use a char[] instead of String[], there is no need of using String to store one character like you do.
Instead of
String s = "a";
you can
char c = 'a';
Reducing the length of the code if you read the next point.
You can reduce your switch by doing some addition like 'a' + r.nextInt(26) to get a value between 'a' to 'z'
For a random symbol, instead of a switch, create a static array of char[] with every character, then get a random value using the size of this array.
Like :
final static char[] symbol = {'#', '#', '-', '\\', '/', '*'};
static getRadomSymbol(){
Random r = new Random();
return symbol[r.nextInt(symbol.length)];
}
Allowing the add of character easily (and reducing again the number of line ;) )

Related

Main method cannot refer to non-static variable [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 2 years ago.
in my public class card, if I do not declare this class as static the program will not run, and all my new card objects will return the following error:
non-static variable this cannot be referenced from a static context
card card1 = new card();
Firstly, my professor has told me that the class should not be static, but has not explained why.
Secondly, I do not understand why the main method (I know it's static) cannot reference a non-static variable.
I think I'm not understanding something fundamental here, but I need to understand what's going on here before moving on.
I've found this definition:
the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type.
I have really no clue what that means. A particular member, is that referring to an object? an instance of that type? also sounds like its referring to an object that belongs to a specific class. To me something that declared static is private, and only other classes that are declared static can operate with each other - is that right? If so, why not just use the word 'private'.
The code:
import java.util.Random;
public class playing_cards {
public class card // sets up class
{
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}
}
public static void main(String[] args) {
card card1 = new card();
card card2 = new card();
card card3 = new card();
card card4 = new card();
card card5 = new card();
card card6 = new card();
card card7 = new card();
card card8 = new card();
card card9 = new card();
card card0= new card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}
You have to separate Card in a new class.
Create a class named Card.java:
import java.util.Random;
public class Card {
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public Card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}
}
Then,create (or use) the Main.java class and write:
public class Main {
public static void main(String[] args) {
Card card1 = new Card();
Card card2 = new Card();
Card card3 = new Card();
Card card4 = new Card();
Card card5 = new Card();
Card card6 = new Card();
Card card7 = new Card();
Card card8 = new Card();
Card card9 = new Card();
Card card0= new Card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}
Your Card class has some strange methods that I didn't understand why you did like this. In general, I corrected some syntax conventions that you didn't respect (like the Camel Case). So, I will not criticize because you are still a student. Good lucky!
As the error says: You cannot refer to non-static variable in static method.
The easiest solution for you would be making another class in different file to which you will move your main method. You can also extract the card class. For example:
PlayingCards class:
public class PlayingCards {
public static void main(String[] args) {
card card1 = new card();
card card2 = new card();
card card3 = new card();
card card4 = new card();
card card5 = new card();
card card6 = new card();
card card7 = new card();
card card8 = new card();
card card9 = new card();
card card0= new card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}
Card class:
public class card // sets up class
{
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}

Switch choosing wrong int case [duplicate]

This question already has answers here:
Switch without break
(7 answers)
Closed 6 years ago.
I have this simple code. An integer which value is 0 and a null String. Simple enough. Yet when I run the program it prints "a" instead of "z". I don't know where's the problem and what am I missing.
public static void main(String[] args) {
int classCode = 0;
String classString = null;
switch(classCode) {
case 0:
classString = "z";
case 10:
classString = "a";
break;
case 11:
classString = "b";
break;
case 20:
classString = "c";
break;
case 21:
classString = "d";
break;
case 30:
classString = "e ";
break;
case 31:
classString = "f";
break;
}
System.out.println(classString);
}
You have forgotten to put a break after the first case.
switch(classCode) {
case 0:
classString = "z";
// missing a break here
case 10:
classString = "a";
break;

Brute force password cracker Java

I am doing an assignment for class which I have to create a brute force password cracker in java.
Write a function using Recursion to crack a password. The password is of unknown length (maximum 10) and is made up of capital letters and digits. (Store the actual password in your program, just for checking whether the string currently obtained is the right password.)
The solution I have is:
import java.util.Arrays;
public class BruteForce {
public static void main(String[] args) {
bruteForce(3);
}
public static String bruteForce(int size) {
int[] password = new int[size];
String[] finalPassword = new String[size];
for (int i = 0; i < size; i++) {
password[i] = 0;
finalPassword[i] = "";
}
String pass = "AAA";
return computePermutations(size, password, 0, pass);
}
private static String computePermutations(int size, int[] password, int position, String pass) {
String testString = "";
String assemble = "";
for (int i = 0; i < 36; i++) {
password[position] = i;
if (position != size - 1) {
testString = computePermutations(size, password, position + 1, pass);
if (testString != "") {
return testString;
}
} else if (position == size - 1) {
for (int j = 0; j < size; j++) {
switch (password[j] + 1) {
case 1:
assemble = assemble + "A";
break;
case 2:
assemble = assemble + "B";
break;
case 3:
assemble = assemble + "C";
break;
case 4:
assemble = assemble + "D";
break;
case 5:
assemble = assemble + "E";
break;
case 6:
assemble = assemble + "F";
break;
case 7:
assemble = assemble + "G";
break;
case 8:
assemble = assemble + "H";
break;
case 9:
assemble = assemble + "I";
break;
case 10:
assemble = assemble + "J";
break;
case 11:
assemble = assemble + "K";
break;
case 12:
assemble = assemble + "L";
break;
case 13:
assemble = assemble + "M";
break;
case 14:
assemble = assemble + "N";
break;
case 15:
assemble = assemble + "O";
break;
case 16:
assemble = assemble + "P";
break;
case 17:
assemble = assemble + "Q";
break;
case 18:
assemble = assemble + "R";
break;
case 19:
assemble = assemble + "S";
break;
case 20:
assemble = assemble + "T";
break;
case 21:
assemble = assemble + "U";
break;
case 22:
assemble = assemble + "V";
break;
case 23:
assemble = assemble + "W";
break;
case 24:
assemble = assemble + "X";
break;
case 25:
assemble = assemble + "Y";
break;
case 26:
assemble = assemble + "Z";
break;
case 27:
assemble = assemble + "0";
break;
case 28:
assemble = assemble + "1";
break;
case 29:
assemble = assemble + "2";
break;
case 30:
assemble = assemble + "3";
break;
case 31:
assemble = assemble + "4";
break;
case 32:
assemble = assemble + "5";
break;
case 33:
assemble = assemble + "6";
break;
case 34:
assemble = assemble + "7";
break;
case 35:
assemble = assemble + "8";
break;
case 36:
assemble = assemble + "9";
break;
}
}
System.out.println(assemble);
if (assemble.equalsIgnoreCase(pass)) {
System.out.println("Password is: " + assemble);
break; //replace this with: return assemble;
} else {
assemble = "";
}
}
}
return "";
}
}
However, when I run the program, the password AAA never seems to work but 998 works just fine, what exactly is wrong?
I think you should use a return statement if a match is found -
if (assemble.equalsIgnoreCase(pass)) {
System.out.println("Password is: " + assemble);
return assemble; // This is missing
}
Your main problem seems to be to break the loop at the right point. Try using a label to break the loop and solve this issue. Find more about labels here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}}

Using a non-constant expression in a switch statement?

I'm quite new to java and have created a Java class that creates a deck of cards, and assigns them a suite, a name, a value and an ID. The problem is, there are 52 different cards that require an ID, and I've been using switch statements to assign the name, value and suite. However, if I were to do this for the card ID, I would need 52 lines of case statements, which is far too many.
public class Deck {
private Card[] cards;
public Deck() {
String suit = null;
String name = null;
int cardID=0;
int value = 0;
cards = new Card[52];
int arrayID=0;
for (int i=1; i<=4; i++){ //number of suites
for (int j=1; j <= 13; j++){ //number of card types
for (int k=1; k==52; k++){ //number of cards
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 11; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
switch (k){
case k: cardID=k; break; //"Case expressions must be constant expressions"
}
Card card = new Card (cardID, name, suit, value);
cards[arrayID] = card;
arrayID++;
}
}
}
}
public void printDeck(){
System.out.println(Arrays.toString(cards));
}
}
I may be doing this whole thing wrong, so are there any other ways I could assign a unique ID to the card without using a switch statement?
There's no point in this switch statement :
switch (k){
case k: cardID=k; break; //"Case expressions must be constant expressions"
}
Just write :
cardID = k;
You could use enum and you could use ordinal value to get each count automatically and you could have property like value in your case and you could do it like:
public enum Cards {
ACE(11), TWO(..), .. JACK(....;
int value;
public int getValue() {return value;}
}
System.out.println(CARDS.ACE.ordinal() + 1);
System.out.println(CARDS.ACE.getValue());
Output:
1
11
You don't need the loop or the switch statement for the cardID. You can synthesize it from i and j
for (int i=1; i<=4; i++){ //number of suites
for (int j=1; j <= 13; j++){ //number of card types
cardId = (i - 1) * 13 + j; // <<< synthesize cardID like this
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 11; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
Card card = new Card (cardID, name, suit, value);
cards[arrayID] = card;
arrayID++;
}
}
What I would do is instead:
Create a class card:
class Card {
private String suit;
private String name;
private int ID;
public Card(String suit, String name, int ID) {
this.suit = suit
(same for name & ID)}}
Then, in your Deck class, you can just have a ArrayList that contains all your card, with some method to add the cards in your deck:
s
class Deck {
private ArrayList<Cards> myDeck = new ArrayList<Cards>();
public void addCards(Card myCard) {
this.myDeck.add(myCard); } }
EDIT: Forgot the variable type in addCards

Switch statement always selects same case [duplicate]

This question already has answers here:
If-else working, switch not [duplicate]
(9 answers)
In a switch statement, why are all the cases being executed?
(8 answers)
Closed 8 years ago.
For the following switch statement:
if a value between 0-9 is selected, output is fine. If value greater than 9 is selected, output is always a lowercase z.
for (int i = 0; i < 3; i++)
{
random[i] = randomnumber.nextInt(36);
if (random[i] > 9)
{
switch(random [i])
{
case 10: character[i] = "A";
case 11: character[i] = "B";
case 12: character[i] = "C";
case 13: character[i] = "D";
case 14: character[i] = "E";
case 15: character[i] = "F";
case 16: character[i] = "G";
case 17: character[i] = "H";
case 18: character[i] = "I";
case 19: character[i] = "J";
case 20: character[i] = "K";
case 21: character[i] = "L";
case 22: character[i] = "M";
case 23: character[i] = "N";
case 24: character[i] = "O";
case 25: character[i] = "P";
case 26: character[i] = "Q";
case 27: character[i] = "R";
case 28: character[i] = "S";
case 29: character[i] = "T";
case 30: character[i] = "U";
case 31: character[i] = "V";
case 32: character[i] = "W";
case 33: character[i] = "X";
case 34: character[i] = "Y";
case 35: character[i] = "Z";
}
}
else
character[i] = Integer.toString(random[i]);
A case statement is a form of standidised goto statement, it goes to the case statement and then continues on as usual. To get the behaviour you desire you need a
break;
at the end of each case
Where is the break of the cases man!
switch(random[i]){
case 10: ....
break;
case 11: ....
break;
//and so on
}
Add break with all case blocks like
case 10:
character[i] = "A";
break;

Categories

Resources