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 2 years ago.
Improve this question
I need to convert these to Java:
Ive tried converting line by line for both and when I run it there is no output when it completes
def count_up_from (a,b):
start = a
end = b
count = start
while count <= end:
print(count , end = ',')
count = count + 1
return count
print(count_up_from(1,5), end = "," "\n")
print(count_up_from(3,6), end = "," "\n")
print(count_up_from(1,2), end = "," "\n")
//
def count_down_to (a,b):
start = a
end = b
count = start
while count >= end:
print(count , end = ',')
count = count - 1
return count
print(count_down_to(5,1), end = "," "\n")
print(count_down_to(6,3), end = "," "\n")
print(count_down_to(2,1), end = "," "\n")
//
I did a rough translation here of count_up_from
Your desired outcome is unclear, so its not possible to do perfectly. This should be enough to help you translate the second.
https://repl.it/repls/CoralOblongSyndrome
class Main {
public static void main(String[] args) {
countUpFrom(1,5);
System.out.println();
countUpFrom(3,6);
System.out.println();
countUpFrom(1,1);
System.out.println();
}
public static void countUpFrom(int a, int b) {
for (int i = a; i <= b; i++) {
System.out.print(i + ",");
}
}
Related
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 3 years ago.
Improve this question
Following is the google interview question, Can someone please solve or provide the logic to find the answer. Operators [+,-,*]
Output should be the list of all the possible strings which generates the target number.
Ex1:
Input = 1234
Desired number = 6
Expected Output = [2+4,3+4-1,2*3,1+2+3... etc]
Ex2:
Input = 105
Desired number = 5
Expected Output = [10-5,1*5... etc]
Here is quick fix for you.
Please check following code.
public static void main(String arg[]) {
String num = "105";
int target = 5;
getValue(num, target);
}
static void check(double sum, double previous, String digits, double target, String expr) {
if (digits.length() == 0) {
if (sum + previous == target) {
System.out.println(expr + " = " + target);
}
} else {
for (int i = 1; i <= digits.length(); i++) {
double current = Double.parseDouble(digits.substring(0, i));
String remaining = digits.substring(i);
check(sum + previous, current, remaining, target, expr + " + " + current);
check(sum, previous * current, remaining, target, expr + " * " + current);
check(sum, previous / current, remaining, target, expr + " / " + current);
check(sum + previous, -current, remaining, target, expr + " - " + current);
}
}
}
static void getValue(String digits, double target) {
for (int i = 1; i <= digits.length(); i++) {
String currentValue = digits.substring(0, i);
check(0, Double.parseDouble(currentValue), digits.substring(i), target, currentValue);
}
}
Output :
1 * 0.0 + 5.0 = 5.0
1 * 5.0 = 5.0
10 - 5.0 = 5.0
Hope this example is help you to understand the concept.
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
so this is the case.
I'm doing an excercise where I have to compare each random number with all my static numbers from an external file lotto.dat
I have to make a method doCompare() which returns either true or false. My question will appear after my code:
public static void drawNumbers()throws Exception{
Random rnd = new Random();
int rndN1 = rnd.nextInt(19)+1;
int rndN2 = rnd.nextInt(19)+1;
int rndN3 = rnd.nextInt(19)+1;
int rndN4 = rnd.nextInt(19)+1;
int rndN5 = rnd.nextInt(19)+1;
int rndN6 = rnd.nextInt(19)+1;
System.out.println();
System.out.println("Winner numbers: " + rndN1 + " " + rndN2 + " " + rndN3 + " " + rndN4 + " " + rndN5 + " " + rndN6);
String match = doCompare(rndN1);
if(match.equals("true")){
System.out.println("Match on the number: " + rndN1);
}
}
So is it possible somehow loop the "doCompare" with parameters "doCompare(rndN1)" then rndN2, rndN3 and so on or what else should I do to make this work?
Use a proper data structure, like an array or a List to store the random numbers and loop over them:
List<Integer> numbers = new ArrayList<>();
for(int cout = 0 ; count < 6 ; ++count) {
numbers.add(rnd.nextInt(19)+1);
}
// ...
for(int n : numbers) { // go through all the numbers in the list
doCompare(n);
}
Create a List which can collect your integers. Create a loop which create the integers and add them to a list. While creating the random integers, you can create the output string in the loop too. And at the end use another loop for the method call doComapre() and pealse change the return value of doCompare() method to boolean. Then you can use it in the if statement and have not to check if the return value is equals "true".
Random rnd = new Random();
List<Integer> rndNumbers = new ArrayList<>();
String outputString = "Winner numbers:";
for(int i = 0; i < 6; i++)
{
rndNumbers.add(rnd.nextInt(19) + 1);
outputString = outputString + " " + rndNumbers.get(i);
}
System.out.println();
System.out.println(outputString);
for(Integer curNumb : rndNumbers)
{
String match = doCompare(curNumb );
if (match.equals("true"))
{
System.out.println("Match on the number: " + curNumb );
}
}
Maybee you can use an array because you are always want to generate six numbers. And for the String creation you can replace the String with a Stringbuilder.
The simplest solution would be to create array or list and store rnd number and then loop over it
Yes, you can, but not as you want to do it.
you have to create a list of your rndNX values
Like so:
List<Integer> rndList = new ArrayList<Integer>();
fill it like so :
rndList.add(rnd.nextInt(19)+1);
rndList.add(rnd.nextInt(19)+1);
...
And use the list :
for(final Integer rndI : rndList)
{
String match = doCompare(rndI );
}
Random rnd = new Random();
System.out.println();
for(int i = 0; i < 6; i++)
{
int rndN = rnd.nextInt(19)+1;
String match = doCompare(rndN);
if(match.equals("true")){
System.out.println("Match on the number: " + rndN1);
}
}
You could do something like this. Instead of initializing all your random numbers first initialize them in a loop as needed.
Store the int values into the array or a list and loop through it.
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 8 years ago.
Improve this question
i have a little missbehaviour in my program.
One of his methods should build an string and then return it to be displayed in an Java Swing GUI.
In some parts of it, it needs to store a "line break" (i dont know the proper name) "/n".
But when i display this string in the gui it contains /n instead of jumping the line properly.
Its a little confusing but with the code i think you can figure it out:
public String draw(Robot robot, MapClass mapa) {
map = mapa.getMapa();
String mapBuffer = "";
for (int i = 0; i < mapa.getLinhas(); i++) {
for (int j = 0; j < mapa.getColunas(); j++) {
if ((i == robot.getPosX()) & (j == robot.getPosY())){
System.out.print(robot.convertToChar() + " ");
mapBuffer = mapBuffer + robot.convertToChar() + " ";
}
else{
System.out.print(map[i][j] + " ");
mapBuffer = mapBuffer + map[i][j] + " ";
}
}
mapBuffer = mapBuffer + "/n";
System.out.println();
}
return mapBuffer;
}
This string is shown in a swing textArea with:
textArea.append(drawer.draw(robot, map));
Thanks.
The problem is here mapBuffer = mapBuffer + "/n"; You are appending to mapBuffer the /n literal. If you want a line break you should replace it with \n
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
I want to have a string variable showing next to my int variable but it doesn't seem to work.
public class ThreeLittleNumbers {
public static void main(String[] argts) {
String as = "Number 1: ";
String bs = "Number 2: ";
String cs = "Number 3: ";
int a = 1;
int b = 3;
int c = 5;
String tot = "Total: ";
System.out.println(as+a);
System.out.println(bs+b);
System.out.println(cs+c);
System.out.println(tot);
System.out.print(a+b+c);
}
}
If I understand your question, then this
System.out.println(tot);
System.out.print(a + b + c);
should be
System.out.print(tot);
System.out.println(a + b + c);
When I make the change above, I get output like you seem to be asking for -
Number 1: 1
Number 2: 3
Number 3: 5
Total: 9
Another option would be to use printf and something like
System.out.printf("%s %d%n", tot, a + b + c);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Hey guys i'm trying to write a code that will count the characters in a text file.
ex : AAABBbbcC
3A 2B 2b 1c 2C
but i don't know how to count the characters like (AAA) if they are not separated.
i'm trying this but it doesn't work like a recursion
String s = "aasjkkk";
int count1 = 0;
int count2 = 0;
char karakter1 = 0;
char karakter2 = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == s.charAt(i+1)) {
karakter1 = s.charAt(i);
count1 += 1;
System.out.print(count1 + " " + karakter1 + " ");
}
else if(s.charAt(i) != s.charAt(i+1)) {
karakter1 = s.charAt(i);
karakter1 = 1;
karakter2 = s.charAt(i+1);
karakter2 = 1;
System.out.print(count1 + " " + karakter1 + " " + count2 + " " + karakter2 + " ");
}
}
Since this looks like a homework, I can suggest a way for you to do this:
Create a HashMap
Loop from the beginning to the end of the file. If you have a new character, then set a new key to the HashMap with 1 as the value; if not, just plus one to the current value of the old character
Have fun !!
It's true that you should and must show what you've tried before.
I think that you could have already thought about taking the String into a char array, iterating, etc...
Well, #bubuzz advise is a good one, here it comes an implementation:
public class ClasesObjetos {
public static void main(String[] args) {
String s = "This is a single String";
HashMap<Character, Integer> m = new HashMap();
for (Character c : s.toCharArray()) {
if (!c.equals(" ")) {
if (m.get(c) == null )
m.put(c, 1);
else {
int i = (int) m.get(c) + 1;
m.put(c, i);
}
}
}
for (Character c : m.keySet()) {
System.out.println(c + " ---> " + m.get(c));
}
}
}
Try it out you'll see it ignores spaces. It will count any symbols though, you should you add up if checks.