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
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 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 + ",");
}
}
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 2 years ago.
Improve this question
I want my fallingTime numbers to be limited to two decimal places. I used "String.format(%.2f)" but it is giving me an error message.
This is the error message:
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:3302)
at fallingDistance.main(fallingDistance.java:22)
public class fallingDistance {
public static double fallDistance(int fallingTime){
double distance;
double g = 9.8;
distance = (0.5*g*Math.pow(fallingTime, fallingTime));
return distance;
}
//public static void displayHeader(){
//System.out.println("Time\t\t\t\t\tDistance\n----\t\t\t\t\t--------");
//}
public static void main(String[] args){
//displayHeader();
for(int time = 1; time <= 10; time++){
System.out.println(time+ " seconds\t\t" + String.format("%.2f", fallDistance(time) + " meters"));
}
}
}
You had misplaced a bracket. The below should work.
for (int time = 1; time <= 10; time++) {
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters");
}
Replace + with ,
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time), " meters"));
I removed " meters" from the end of the code and put it after "%.2f" and that worked. The code looks like this now:
{System.out.println(time + " seconds\t\t" + String.format("%.2f meters", fallDistance(time)));}
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
My program keeps adding up the score for each player, rather than keeping it seperate for example if first player gets 3/5 and the second gets 2/5 the score display for the second player will be 5. I know the answer is probably very simple however I'm not able to find it within the code.
Upfront thanks!
public static void questions(String[] question, String[] answer, int n) {
String[] name = new String[n]; // Player Names
int[] playerscore = new int[n]; // Argument for Score
String[] que = new String[question.length]; //Questions for Loops
int score = 0; // Declare the score
/* --------------------------- For loop for number of players --------------------------- */
for (int i = 0; i < n; i++)
{
name[i] = JOptionPane.showInputDialog("What is your name player"+ (i+1) +"?");
JOptionPane.showMessageDialog(null,"Hello :"+ name[i] + " Player number " +(i+1)+ ". I hope your ready to start!");
/* --------------------------- Loop in Loop for questions --------------------------- */
for (int x=0; x<question.length; x++) {
que[x] = JOptionPane.showInputDialog(question[x]);
if(que[x].equals(answer[x]))
{
score = score +1;
}
else {
JOptionPane.showMessageDialog(null,"Wrong!");
}
} // End for loop for Question
playerscore[i] = score;
System.out.println("\nPlayer"+(i)+ "Name:"+name[i]+"\tScore"+score);
Reset the score variable within your loop and then place it in the corresponding element of playerscore. Here's the code:
for (int i = 0; i < n; i++){
name[i] = JOptionPane.showInputDialog("What is your name player"+ (i+1) +"?");
JOptionPane.showMessageDialog(null,"Hello :"+ name[i] + " Player number " +(i+1)+ ". I hope your ready to start!");
score = 0; //reset the score variable
/* --------------------------- Loop in Loop for questions --------------------------- */
for (int x=0; x<question.length; x++) {
que[x] = JOptionPane.showInputDialog(question[x]);
if(que[x].equals(answer[x])){
score = score + 1;
System.out.println("\nPlayer"+(i)+ "Name:"+name[i]+"\tScore"+score);
}
else{
JOptionPane.showMessageDialog(null,"Wrong!");
}
} // End for loop for Question
playerscore[i] = score; //assign the score for each player
}
Then whenever you want the score for name[i] you can just print playerscore[i]
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.