Why do I need the abs method

No description
23 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @blue! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3mo ago
if a is >10, abs() turns the result into a positive number similar with b abs(10-a) calculates how much a is away from 10 if it wasn't there, it would just prefer the highest number no matter what
blue
blueOP3mo ago
I wrote that part of the code from the exercise but abs was given therefore my question but the number just stays the same if it is negative it will return that number and if it is positiv it will return a positive number does abs really change anything @dan1st | Daniel
dan1st
dan1st3mo ago
if a number is negative, it returns the positive version e.g. abs(-5) is 5
blue
blueOP3mo ago
but shouldn`t it be return a if that was true it is -a in the code though that confuses me
dan1st
dan1st3mo ago
-(-5) is 5, right? -a changes the sign
blue
blueOP3mo ago
aaahhh hhaha
dan1st
dan1st3mo ago
so if it's a negative number, the sign is changed to be positive
blue
blueOP3mo ago
thanks a bunch got totallly confused there!!
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
blue
blueOP3mo ago
Coulf I ask you another question if you have time
blue
blueOP3mo ago
haha got it thanks just one sec need to copy the code
john
john3mo ago
package de.codegym.task.task04.task0413; /* Wochentag / import java.io.; import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
//schreib hier deinen Code

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int a = input.read();// read a number from keyboard

//Scanner numberkind = new Scanner(System.in);
//int a = numberkind.nextInt();


if (a ==1 )
System.out.println("Montag");
else if (a == 2 )
System.out.println("Dienstag");
else if (a == 3 )
System.out.println("Mittwoch");
else if (a == 4 )
System.out.println("Donnerstag");
else if (a == 5 )
System.out.println("Freitag");
else if (a ==6 )
System.out.println("Samstag");
else if (a == 7 )
System.out.println("Sonntag");

else if ( a > 7 || a < 1) //condition always true
// since if reached only option bigger or less
// then given intervall
//=> just else also enough
System.out.println("Diesen Wochentag gibt es nicht");

}
}
public class Solution {
public static void main(String[] args) throws Exception {
//schreib hier deinen Code

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int a = input.read();// read a number from keyboard

//Scanner numberkind = new Scanner(System.in);
//int a = numberkind.nextInt();


if (a ==1 )
System.out.println("Montag");
else if (a == 2 )
System.out.println("Dienstag");
else if (a == 3 )
System.out.println("Mittwoch");
else if (a == 4 )
System.out.println("Donnerstag");
else if (a == 5 )
System.out.println("Freitag");
else if (a ==6 )
System.out.println("Samstag");
else if (a == 7 )
System.out.println("Sonntag");

else if ( a > 7 || a < 1) //condition always true
// since if reached only option bigger or less
// then given intervall
//=> just else also enough
System.out.println("Diesen Wochentag gibt es nicht");

}
}
This message has been formatted automatically. You can disable this using /preferences.
blue
blueOP3mo ago
I used the Scanner and always got the result I was trying to achieve but with BufferReader I only get the output "Diesen Wochentag gibt es nicht" even if I enter 5 Can´t quite find my fault here
dan1st
dan1st3mo ago
read() doesn't read a number
blue
blueOP3mo ago
Should I use readInt
dan1st
dan1st3mo ago
it reads a character and converts it to a number Which readInt? If you use Scanner, you could use nextInt or you could use BufferedReader#nextLine and then convert it to an int using Integer.parseInt
blue
blueOP3mo ago
Yup that version worked but with BufferReader no clue Ah got it will try it Javadoc things Thanks a lot you really cleared up a lot for me!!
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3mo ago
this isn't about Math.abs() btw
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server