1. Originally Posted by sunde887
An easier way is to simply subtract the low end of the number from the total(for 6.5, the low end is 6.) with this you will be able determine what the right side of the decimal is and do comparisons.
how do you mean? If I dont know what the number is(it will be written from console), then I cant know if it is 6.5

Also, about rounding, since java dont do it by itself, it would be best to use this:
: Class Rounding
I have imported java.lang.Object and java.math.*, but I still cant use round method.

Java Code:
```C:\New\Pojahns.java:13: cannot find symbol
symbol  : method round(double,int)
location: class Pojahns
double tal2 = round (tal, 0);
^
1 error

Tool completed with exit code 1```

2. Round is a static method in main so you use it like this
Java Code:
`Math.round(number);`
As to the other part, you don't know the number they are gonna enter but you have a variable to store the input in. Let's say you name it n, you can do calculations on n.

See what
Java Code:
`System.out.println(n - (int)n);`

3. Originally Posted by sunde887
Round is a static method in main so you use it like this
Java Code:
`Math.round(number);`
As to the other part, you don't know the number they are gonna enter but you have a variable to store the input in. Let's say you name it n, you can do calculations on n.

See what
Java Code:
`System.out.println(n - (int)n);`
Thanks. System.out.println(n - (int)n); is very intresting. I will play around with it. I can now create my own rounding(removing decimals). Like this:

Java Code:
```class Pojahns

{
public static void main (String[] args)// throws Exception
{
java.util.Scanner	in = new java.util.Scanner (System.in);

System.out.println ("Write a number you want to round:");
double tal1 = in.nextDouble ();
double deci = tal1 - (int)tal1;
int tal2 = 0;

if (deci >= 0.5)
{
tal1 += 1;
tal2 = (int) tal1;
}

else
tal2 = (int) tal1;

System.out.println (tal2);

}
}```
:D
Last edited by Pojahn_M; 03-30-2011 at 04:03 PM.

4. Very odd situation, I think something have broke.

Java Code:
```int[] v = {10};

System.out.println (v);```
This prints:

[I@158b649
All I have done is to add a new folder in C:\Program\Java\jdk1.6.0_23\jre called classes. In there i have other folders called pjjava->nbie->switcheru.class.
Nothing else. wth?

5. I don't know why you mentioned the folders you added. This is easily understood.

The weird gobbledegook you see is the default toString method. Which prints
Java Code:
`ClassName@afed135`
the numbers are a hexadecimal representation of the hash code. When you want to print arrays you must print each individual item in the array.

6. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6
Originally Posted by Pojahn_M
Very odd situation, I think something have broke.

Java Code:
```int[] v = {10};

System.out.println (v);```
This prints:

All I have done is to add a new folder in C:\Program\Java\jdk1.6.0_23\jre called classes. In there i have other folders called pjjava->nbie->switcheru.class.
Nothing else. wth?
System.out.println accepts variable values, not array values.
That would be done correctly by changing the System.out.println to:
Java Code:
`System.out.println(v[0]);`

7. To clarify my point, print either prints primitive items directly, or for objects it prints the toString method.

8. Stuck on a minor thing.
Using Math.random, I want to create a random string(with random length, no decimals), that is numbers(0-9).

I tried many diffrente types of codes, I cant get it to work.

9. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6
Originally Posted by Pojahn_M
Stuck on a minor thing.
Using Math.random, I want to create a random string(with random length, no decimals), that is numbers(0-9).

I tried many diffrente types of codes, I cant get it to work.
Java Code:
```import java.util.Random;

Random r = new Random();

String s = r.nextInt(10).toString();```
Last edited by Solarsonic; 04-06-2011 at 09:21 PM.

10. Intresting. Can you explain it?
it does not work.

C:\New\Pojahns.java:18: int cannot be dereferenced
String s = (String)r.nextInt(10).toString();
^
1 error

Tool completed with exit code 1
Last edited by Pojahn_M; 04-06-2011 at 09:45 PM.

11. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6
Originally Posted by Pojahn_M
Intresting. Can you explain it?
it does not work.
Change it to:

Java Code:
```import java.util.Random;

Random r = new Random();

String s = String.valueOf(r.nextInt(10));```
Last edited by Solarsonic; 04-06-2011 at 09:52 PM.

12. Originally Posted by Solarsonic
Change it to:

Java Code:
```import java.util.Random;

Random r = new Random();

String s = String.valueOf(r.nextInt(10));```
that works great.

Java Code:
`nextInt(10)`
Basically I get everything in your code except that part above. Well I get that in this situation it means that 10 is the max value that can be generated.
But I dont get it.

If it means that 10 is max value generated, how come this wont work:

Java Code:
`int i = in.nextInt (10); //You cant enter a value higher than 10`

13. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6
Originally Posted by Pojahn_M
that works great.

Java Code:
`nextInt(10)`
Basically I get everything in your code except that part above. Well I get that in this situation it means that 10 is the max value that can be generated.
But I dont get it.

If it means that 10 is max value generated, how come this wont work:

Java Code:
`int i = in.nextInt (10); //You cant enter a value higher than 10`
What you're forgetting is that Java starts at 0. The "10" parameter is telling it to get the first 10 numbers starting from 0 (Java always starts from zero, like with arrays for example).

You can also do
Java Code:
`int i = in.nextInt (10); //You cant enter a value higher than 10`

Two other ways of doing this would be:
Java Code:
`String s = (String) in.nextInt(10);`
or
Java Code:
`String s = in.nextInt(10).toString();`
So there's 3 ways to do this.

14. Another way, that I would have suggested would be to have something like this

Java Code:
```String[] letters = "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v" +
"W w X x Y y Z z".split(" ");```
This would give you an array containing every english alphabet letter. Then you can just randomly index into this array with a loop to build a String.

By the way, how does solars method accomplish what you want pojahn? I believe you said you wanted a random string of letters, not including numbers. All solars method does is get you a string representation of a number.
Last edited by sunde887; 04-07-2011 at 01:22 AM.

15. Another way to do this is to generate a random number from 96-(96+26) (check the ASCII table to make sure that's the correct range and cast it to a char, then concatenate it and do this in a loop x times. To get capitals you can do something like this

Java Code:
```if(Math.random() > .5){
Create random number in range of lower case letters
}
else{
Create random number in range of capital letters
}```

16. actully, I wanted numbers only. But your first suggestion, string array is probably the best one. I did not know that array string was possible(my book never said anything).
I can just modify that string so it contains numbers, and get a random numer like this:
Java Code:
`int a = letters[(int) (10 * Math.random())];`
That should work.

17. Banned
Join Date
Nov 2010
Posts
44
Rep Power
0
Split is String method. Consult the Sun Java API for more information on the String class:
String (Java Platform SE 6)

18. Im a bit confused what you want, if you want a random number as a string, it's probably easiest to just generate a random number as usual then convert to a string like this

Java Code:
```int num = generate some random number
String anotherNumber = num + "";```

19. I am having a hard time understanding the library: Java Platform SE 6
I have searched on google but couldnt find anything.
Lets look into java.lang.StringBuffer.

On the first picture below, I know what a constructor is, but Charsequence seq? Can you please give an example on how this look? Also what do they mean with capacity?

On the second picture:
- what does the left column mean? Is it just info or does it decide how I will write the codes?
- How do I know if when the argument is infront of the method and in the parentheses?

The rest seems pretty clear I think. My biggest problem is the left column on pic2. I just dont get it.

20. A stringBuffer is a class that lets you handle strings more efficiently. I believe StringBuilder is preferred over StringBuffer. In the first picture the CharSequence and String are arguments to the constructor. if you pass a String argument to the buffer, or builder, it will contain the string.

Java Code:
`StringBugger sb = new StringBuffer("Hello"); //contents of sb are now "Hello"`
the charsequence argument takes a character sequence. Go here to read on charSequences:
CharSequence (Java Platform SE 6)

You would generally use a stringbuilder when concatenating strings. If you decompile java code that appends strings you can see that appending String objects if inefficient. If you are appending multiple times(using a loop to build a string), it's much better to use a StringBuilder or StringBuffer.

On the second picture the left argument is the return type. For many of those methods it simply appends something to the end of the string buffer and returns the string buffer object.

for this
Java Code:
```return type method name(type arg)

is

StringBuffer append(String str)```
This appends the String argument to the string buffer object and returns the stringbuffer with str.length() extra characters.

The API description of capacity:
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

The best way to test it is to create a string buffer like this and prnt the capacity.

Java Code:
```StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());```
Last edited by sunde887; 04-13-2011 at 12:23 AM.

Page 3 of 5 First 12345 Last

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•