[Solved] The shortest java code [closed]

Almost correct: System.out.println((int)Math.signum(input.nextInt() – input.nextInt()); “Almost” due to possible integer overlow. Also your longer code might actually be faster (signum() operaton on floating-point numbers), not to mention more readable. 1 solved The shortest java code [closed]

[Solved] how to solve this type of compilation error?

Make all of your fields static if you are just calling them from the Main class. You can’t call an instance field from a static position, as you aren’t currently in an instance. If you wish to keep them non-static, you have to make an instance of DjPageDownloader by putting it in a constructor, and … Read more

[Solved] How to get output with println()

If you want to use 0 and 1, here you go: Scanner scanner = new Scanner(System.in); boolean p = scanner.nextInt() != 0; boolean q = scanner.nextInt() != 0; boolean r1 = p && q; boolean r2 = q == r1; boolean r3 = p == r2; System.out.println(r3 ? 1 : 0); solved How to get … Read more

[Solved] Split the number on a decimal

public static void main(String[] args) { String str = “154.232”; str = str.replaceAll(“\\..*”, “”); System.out.println(str); } or str.substring(0, str.indexOf(“.”)); or // check for index of . is not -1, then do following. str.split(“.”)[0]; output 154 solved Split the number on a decimal

[Solved] Which of the following classes in java library do not implement a design pattern?

The official source for the Java standard library is the standard Java API documentation. And one notable source for design patterns is the book Design Patterns: Elements of Reusable Object-Oriented Software. To begin with, when you look at the options, the question (as quoted in your post) is badly formulated: “Which of the following classes … Read more

[Solved] Rearrange the string

Before you do sss+=str;,please add sss=str;. you forget to update your sss. public static void main(String[] args) { String s = “2a[2b[c]]]”; Stack s1 = new Stack(); for(int i=0;i<s.length();i++) { s1.push(s.charAt(i)); } String str=””; String sss=””; for(int j=0;j<s.length();j++) { char a = (char)s1.pop(); System.out.println(“a:”+a); // if(a == ”) if((int)a >=49 && (int)a<=58){ for(int i=0;i<(int)a-48;i++){ sss=str; … Read more

[Solved] Program is saying all my valid inputs are invalid (REGEX code Issue maybe?) [closed]

I think you need to add Pattern. import java.util.*; public class lab2q2 { public static void main(String[] args) { String RegularExp = “((Mr|Ms))?[A-Z][a-z]+([A-Z]([a-z]+\\.))?([A-Z](a-z)+)”; Pattern pattern = Pattern.compile(RegularExp); Scanner keyboard = new Scanner(System.in); for(int i = 0; i< 11; i++) { System.out.println(“Please enter a name: “); String inputString = keyboard.nextLine(); Matcher matcher = pattern.matcher(inputString ); if(!matcher.matches()) … Read more