Win a copy of Head First Go this week in the Go forum!

Predrag Djindjic

Greenhorn
+ Follow
since Mar 10, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Predrag Djindjic

I have actually done it with switch instead, but i get an error every time i try to do a multiplication('*').Why is that?
import java.util.Locale;

public class DN03 {
   public static void main(String[] args)throws Exception{
       Locale.setDefault (Locale.ENGLISH);
       int a=Integer.parseInt(args[0]);
       int b=Integer.parseInt(args[2]);
       String operation=args[1];
       switch (operation){
           case "+":
               double zbir=a+b;
               System.out.printf("%d + %d = %.2f",a,b,zbir);
               break;
           case "-":
               double razlika=a-b;
               System.out.printf("%d - %d = %.2f",a,b,razlika);
               break;
           case "*":
               double proizvod=a*b;
               System.out.printf("%d * %d = %.2f",a,b,proizvod);
               break;

       }

   }
}
1 week ago
Hi, the task im currently solving goes like this:
Write the DN03.java program with which you can perform basic computational operations (+, -, #, /, ^)
he results of all operations should be written to 2 decimal places, as shown below:
Example 1: java DN03 5 + 7

5 + 7 = 12.00

Im trying to start with sum but i cant get to even print the sum when i detect it.Any help?
public class DN03 {
   public static void main(String[] args)throws Exception{
       for(int i=0;i<args.length;i++){
           char suma='+';

           if(args[i].equals(suma)){
               int a=Integer.parseInt(args[0]);
               int b=Integer.parseInt(args[2]);
               System.out.println(args[0] + "+" + args[2] + "=" + (a+b));
           }
       }
   }
}
1 week ago
Thanks for all the replies, I solved it with string formatting.
1 week ago

2 weeks ago

Campbell Ritchie wrote:Welcome to the Ranch

Unfortunately there isn't a straightforward centred option for printing to the console. I am afraid you are gong to have to count characters in your words and calculate the spacing. Let's imagine you have a nine‑letter word to centre in 20 keystrokes' wdth. Use one places for the left *, and that leaves 19. Subtract (9 + 1), the 1 being for the right *. That leaves 9; half of 9 is 4, so try printf:-Alternatively, create a StringBuilder object and append those spaces to it.

You will need to expand my code to get it to work.




But the problem is,i have to make a for loop that will do that for any given word.For example,if I type "Java is cool", the biggest word has 4 characters,the width is the length of that word +4 because i have to give it a space on each side and that a '*' on each side too.So for every next word the spacing can be calculated by (width-current word)/2 but after that i cant align the remaining star.Look at my code at given picture and tell me if I messed something up.
2 weeks ago
Hi! I am a beginner in Java programming language and i stumbled upon a problem i cannot solve.It goes like this:
Write the DN02 program , which will receive the words received in the arguments, framed and displayed on the screen. Example: when calling a program:
java DN02 That's it!    let the program print:
*******
*    To  *
*    je  *
*    to! *
*******
The text that the program prints in the box must be aligned centrally (shorter words must be aligned in the middle of the space).

As you can see in a given picture, i cant make the right side align the right way.Maybe i should solve it with string formatting but i dont know how.Can someone help me?
Thanks
2 weeks ago