Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Simple password maker
Random myrand = new Random();
int x = myrand.nextInt(126);
x = x+1;
char c = (char)x;
//this gets the character version of x
for (int mycounter = 0; mycounter < 10; mycounter++)
{
x = myrand.nextInt(127);
x = x+1;
if (x == 32)
{
x = 33;
}
c = (char)x;
System.out.print(c);
}
System.out.println();
Where is my error?
How do I fix this?
Currently you're detecting the space character, but what about other characters which aren't printable? I wouldn't expect anything lower than ASCII 32 to be useful as a password.
I suspect you want something like:
// Remove bit before the loop which used x and c. It was pointless.
Random myrand = new Random();
for (int mycounter = 0; mycounter < 10; mycounter++)
{
// Range [33, 127)
int x = myrand.nextInt(127 - 33) + 33;
char c = (char) x;
System.out.print(c);
}
Rather than map "everything non-printable" to a single character, the code above just avoids picking it in the first place, by restricting the range further.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 29 days ago.
Improve this question
As part of a homework assigment I was tasked to printout this shape here: LinkToTask
But in the code I made which can be found below, the nested for loop seems to loop infinetley and after reading over and making changes to the code several times, nothing has seemed to change.
for(int y = 1; y <= 7; y++) // y-cords
{
for(int x = 1; y <= 7; x++) // x-cords
{
if(y == 1 || y == 7 || x == 1 || x == 7) // If one of the cords lies on an edge, print out #
System.out.print("#");
else
System.out.print(" "); // Blank filler
}
System.out.println(); // Next line of shape
}
In your inner for loop, the condition looks at y instead of x.
for(int x = 1; y <= 7; x++)
There is an y instead of an x
for(int x = 1; x <= 7; x++) // x
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In Java, the following code:
long x = 123;
String s = "abc" + x;
takes a significantly more runtime than:
long x = 123;
String s = "abc" + String.valueOf(x);
I got to know this through leetcode. I was trying to solve the following problem: https://leetcode.com/problems/fraction-to-recurring-decimal/
Here is the exact code for my solution:
public String fractionToDecimal(int numerator, int denominator) {
long n = numerator, d = denominator;
boolean isNegative = (n * d < 0);
if(n < 0) n = -n;
if(d < 0) d = -d;
long q = n / d;
long r = n % d;
if(r == 0) return (isNegative ? "-" : "") + q;
StringBuilder sb = new StringBuilder();
if(isNegative) sb.append('-');
sb.append(q).append('.');
Map<Long, Integer> found = new HashMap<>();
int index = sb.length();
while(r > 0 && !found.containsKey(r)){
found.put(r, index++);
n = r * 10;
q = n / d;
r = n % d;
sb.append(q);
}
if(r > 0) {
sb.insert(found.get(r), "(");
sb.append(')');
}
return sb.toString();
}
When I click on Submit it takes as long as 7 milliseconds to complete.
But if I literally just change line no. 8 from + q to + String.valueOf(q) the runtime plummets down to just 1 millisecond. Please feel free to copy paste the code on leetcode to try it out there and see this change in runtime for yourself, if necessary.
This is highly confusing to me. Why is this happening? As per my understanding, in both the cases, compiler first converts the long to a String, and then concatenates those two Strings together, right? So, under the hood, isn't concatenating a String and a long exactly the same as concatenating two Strings together? Then why does one take more time to run than the other? Any insight would be highly appreciated. TIA.
Note: this answer was written before the question was changed. It used to include the expressions shown below.
"abc" + 123 is a constant expression - the concatenation is done at compile-time so "abc123" ends up in the constant pool.
"abc" + String.valueOf(123) is not a constant expression. The concatenation happens at execution time, which is obviously rather more expensive than just using the compile-time concatenation result.
So I'd expect the result to be the opposite of what you've actually reported in the question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
// a program tells the maximum value we can store in (unsigned integer) a given bits .( 256 for 8 bits)
int counter=0;
int last= 0b11111111;
for(int first=0b00000000;first<=last;counter++)
{
first=first + 1;//adding 1(binary addition)
}
System.out.println("for "+ variable "bits u can store "+counter values");
//variable here 8.
//(1.how to get it from user? 2.how to convert it into binary 0b00000000?)
//how to do this without 0b ,actually in previous version of java
//a program in which if you give 8 bit(in case of unsigned) then it give u maximum values u can store in it, not by using ((2*n)-1).
//code is not only for java 8
// sorry i do not have java 8 i hope the above code will compile without error
thank you in advance
Try this:
long result = 1 << numBits;
If numBits is greater than the size of long, use a double instead (and cast the "1" and "numBits" to double).
how about this
int last = 0;
for(int i = 0; i < bitNum; i++){
last = (last << 1) + 1
}
Scanner s = new Scanner(System.in);
int variable = s.nextInt();
int counter = 0;
long last = (1 << variable) - 1;// = 0b'111....111
for(int first = 0; i <= last; counter++){
first = first + 1;
}
System.out.println("for "+ variable +"bits u can store " + counter + "values");
this code is same with your code, but this code dont use 0b.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
this is a question which needs to be answer for a job interview, I only know how to do the following:
int x = y/2;
is there any alternatives ?
Shift right by 1 bit:
int x = y >> 1;
Just to let the interviewer know that you're up for the fun:
int x = 0;
for(int i = 0; i < y; i += 2)
{
x++;
}
Of course, you'll need to do some additional stuff for a negative number, but you get the drill ;)
a << 1 is the same as a * 2. And a >> 1 is the same as a/2
Well as Drakosha said shifting right by 1 :
int x = y >> 1;
or
multiply by 0.5:
int x = (int)(y * 0.5);
or
subtract by the value multiplied by 0.5:
int x = (int)(y - (y * 0.5));
If you don't want to use shifting of bits you could even keep on subtracting the number by 2 till you get the remainder less than 2. Keep a count of how many times you subtracted. Plain maths.
As said, you also could shift the number bitwise. The operator is >> or <<.
Look this example, should make everything clear:
int x = 16;
x = x >> 1;
System.out.println(x); // prints 8
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Write a program that returns number of decimal digits in the given input string.
ex :
Input: 2345abc423
output: 7
This is my code:
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int len = s.length();
int numbers = 0 ;
for(int i=0; i < len; i++){
char c = s.charAt(i);
int num = (int) c;
while(num <= 57 && num >= 48){
System.out.println(numbers);
numbers++;
}
}
System.out.print(numbers);
/////
sorry because my question wasn't obvious
my program always return 0 , not the number of decimal digits
thanks #Keppil i tried if and it worked
thanks
Your program will never exit the while loop since the condition never changes. You should just use an if instead:
if (num <= 57 && num >= 48) {
System.out.println(numbers);
numbers++;
}
Solution that abuses regex:
System.out.println(input.replaceAll("\\D", "").length());