[Solved] I’m learning about Java factory methods and want to know how to implement the example provided

GameType looks like an enum or a class with something that simulate an enum, so I would do that. So it will be something like : public enum GameType { Poker, BlackJack; } Which give something like in your example : public class Main { public static void main(String[] args) { GameType type = GameType.Poker; … Read more

[Solved] I cannot process chars [closed]

Instead of setting a character variable which is discarded, I assume you want to use this character to build a new String. StringBuilder sb = new StringBuilder(); for (char ch : dna.toCharArray()) { switch (ch) { case ‘A’: sb.append(‘T’); break; case ‘T’: sb.append(‘A’); break; case ‘G’: sb.append(‘C’); break; case ‘C’: sb.append(‘G’); break; } } String … Read more

[Solved] If else statements and subStrings (new to programming in Java)

You should print the prompt before you accept user input, otherwise you won’t know what you are entering. For example, in your code String Destination = S.nextLine(); System.out.println(“Specify :” + Destination); That first line will halt and wait for output with no prompt as what to enter. Changing around a few lines should get you … Read more

[Solved] unable to get the expected output,i should get true but when executed i am getting false?

you just need to change you code likewise, if(str.substring(1,3).equals(a)){…} what you did wrong is , you have been used ‘==’ assignment operator, it is not comparing content(here, string) instead of it, it compare’s memory location between two compared string. it’s obvious to get false, because how is it possible ? that two different string refer … Read more

[Solved] One java file use another java file compile error

If you really want to manually call the compiler, make sure to build in a proper output directory (not .), use it as with a -classpath argument, and compile the classes in the right order (TreeNode before FPTree). Or use ant, maven, or an IDE. 0 solved One java file use another java file compile … Read more

[Solved] How do] I call one method from a class to another class? [closed]

It Should be something like that: public class MyClass { public void setSerial(int ser) { serialNumber = ser; } public double computeArea() { return width*length; } } But that won’t work: public void displayBoxes(){ MyClass myClass = new MyClass(); DecimalFormat df = new DecimalFormat(“0.000”); System.out.println(toString()); System.out.println(“The rectangle with the serial number ” + myClass.setSerial(12345) + … Read more

[Solved] Only allowing numbers to be taken from user input in java

Inside your while loop use the following code: System.out.print(“Enter the test score: “); while (!keyboard.hasNextInt()) {//Will run till an integer input is found System.out.println(“Only number input is allowed!”); System.out.print(“Enter the test score: “); keyboard.next(); } int tS = keyboard.nextInt(); //If input is a valid int value then the above while loop would not be executed … Read more

[Solved] How to choose proper if statements in linked list?

Consider the following Linked List: HEAD —> [“value2” | -]–> [“value3” | -]–> [“last_value” | null] Assuming current points to “value3”: HEAD —> [“value2” | -]–> [“value3” | -]–> [“last_value” | null] ↑ then current.next != null is true, since current.next is actually the “next” node. Now let’s assume current moved to “last_value”, now we’ll … Read more

[Solved] how to increment value of string variable?

A little bit simpler : public class Count { private static int count = 0; // resets the value to 0 public static void reset() { count = 0; } // updates the value public static String update() { count++; if(count == 1000) count = 1; return String.format(“%03d”, count); } // main() is used for … Read more

[Solved] Creating a class with all of my functions in java

@Quincunx’s answer in the comments is correct, but writing whole programs like this violates all sorts of OO principles, and it’s not a good idea for readability, maintainability, etc. You probably want to go back and read some basic Java tutorials. For example, to use a method outside of the class that declares it, you … Read more