I need some more help with JAVA.

SithSolo1

Diamond Member
Mar 19, 2001
7,740
11
81
For new stuff to help me on, see 12-13th post down depending on how you count


This may be basic stuff to you but I'm gonna need some help(What can I say, I'm a hardware guy).

Ok, I'm trying to print something to the console so I'm using System.out.print and System.out.println Now my current problem is that its cutting word off in the middle and going to the next line. Is there anyway to fix this(maybe another command) or do I have to add spaces?

I'm postive I will have more problems later but this is the one I'm working on now.

Thx
J
 

BDawg

Lifer
Oct 31, 2000
11,631
2
0
Originally posted by: SithSolo1
This may be basic stuff to you but I'm gonna need some help(What can I say, I'm a hardware guy).

Ok, I'm trying to print something to the console so I'm using System.out.print and System.out.println Now my current problem is that its cutting word off in the middle and going to the next line. Is there anyway to fix this(maybe another command) or do I have to add spaces?

I'm postive I will have more problems later but this is the one I'm working on now.

Thx
Brian

I've never experienced this problem...then again, I always combine all of my strings into one strings, put in my own breaks and then use the System.out.print, so I'd be something like this

String String1 = "";
String1 += "this is ";
String1 += "the first line";
String1 += "\n";
String1 += "this is ";
String1 += "the second line";
System.out.print String1;

That way, I'm in charge ofall of the formatting.
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0
You'll need to clarify (ie copy/paste it into a reply). If you have the line return switch in there ( \n ) then it will go to the next line.
 

manly

Lifer
Jan 25, 2000
13,566
4,233
136
Originally posted by: BDawg

I've never experienced this problem...then again, I always combine all of my strings into one strings, put in my own breaks and then use the System.out.print, so I'd be something like this

String String1 = "";
String1 += "this is ";
String1 += "the first line";
String1 += "\n";
String1 += "this is ";
String1 += "the second line";
System.out.print String1;

That way, I'm in charge ofall of the formatting.
Since String is immutable, this is horribly inefficient for any production code.
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
Originally posted by: manly
Originally posted by: BDawg

I've never experienced this problem...then again, I always combine all of my strings into one strings, put in my own breaks and then use the System.out.print, so I'd be something like this

String String1 = "";
String1 += "this is ";
String1 += "the first line";
String1 += "\n";
String1 += "this is ";
String1 += "the second line";
System.out.print String1;

That way, I'm in charge ofall of the formatting.
Since String is immutable, this is horribly inefficient for any production code.


Coupled with the fact that Java is a horribly inefficient language, that code could take forever to run.
 

BDawg

Lifer
Oct 31, 2000
11,631
2
0
Originally posted by: manly
Originally posted by: BDawg

I've never experienced this problem...then again, I always combine all of my strings into one strings, put in my own breaks and then use the System.out.print, so I'd be something like this

String String1 = "";
String1 += "this is ";
String1 += "the first line";
String1 += "\n";
String1 += "this is ";
String1 += "the second line";
System.out.print String1;

That way, I'm in charge ofall of the formatting.
Since String is immutable, this is horribly inefficient for any production code.

How would you recommend doing this? I've never had any performance issues. Then again, I've never written any enterprise level code.
 

SithSolo1

Diamond Member
Mar 19, 2001
7,740
11
81
public class ConsoleIOTest {
public static void main(String[] args){

System.out.println("This program is designed to calaculate how many boxes of Oaties breakfast cereal are contained in one metric ton and the weight of one box of Oaties in metric tons. It will also calculate the number of whole cases and extra boxes of Oaties in one metric ton.");
System.out.println();
System.out.println("All of this will be done using the weight of one box of Oaties supplied by you.");

Thats how I'm doing it which is more than likely very wrong.

I did the 2nd System.out.println just to skip a line, i dunno how else to do it
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
There are a couple of ways to fix that.

you can just put a '\n' (without quotes) where ever you want a new line:

public class ConsoleIOTest{
public static void main(String[] args){

System.out.println("This program is designed to calaculate how many boxes of Oaties breakfast \ncereal are contained in one metric ton and the weight of one box of Oaties in \nmetric tons. It will also calculate the number of whole cases and extra boxes \nof Oaties in one metric ton.");
System.out.println();
System.out.println("All of this will be done using the weight of one box of Oaties supplied by you.");
}}


or you can use a separate SOP for each line:

public class ConsoleIOTest{
public static void main(String[] args){

System.out.println("This program is designed to calaculate how many boxes of Oaties breakfast");
System.out.println("cereal are contained in one metric ton and the weight of one box of Oaties in");
System.out.println("metric tons. It will also calculate the number of whole cases and extra boxes");
System.out.println("of Oaties in one metric ton.");
System.out.println();
System.out.println("All of this will be done using the weight of one box of Oaties supplied by you.");
}}

there are other ways too, but this should get you started.
 

manly

Lifer
Jan 25, 2000
13,566
4,233
136
Originally posted by: Yomicron
Since String is immutable, this is horribly inefficient for any production code.


Coupled with the fact that Java is a horribly inefficient language, that code could take forever to run.
Okay, do all of your coding in ASM then. Perhaps you're just being facetious, but your statement shows no understanding that languages themselves aren't usually inherently slow or fast; it's their runtime implementations that determine performance.


BDawg,

The problem with the code idiom you used is that String concatenation (performed in the += operator) is relatively expensive. The simplest workaround is of the form:

String str = "I can contenate " + "multiple separate strings " + "at compile time " + "to eliminate the extra overhead.";

In this case, the concatenations are optimized away by the compiler, leaving just the long String constant in the bytecode. This optimization does not occur if a non-final variable is one of the operands.

Basically, as with any optimizations, don't worry about it for casual code you write. It's most likely not going to affect the overall system performance. However, where String concatenation will kill your system is if you carelessly integrate it into a core system component that gets used a lot.

As one general example, if you implemented a logging system so that the rest of your code would write warnings, errors, etc. into a centralized location, careless String concatenation buried within your logging API could become a system bottleneck.
 

SithSolo1

Diamond Member
Mar 19, 2001
7,740
11
81
===== denotes the start and end of a problem area.

public class ConsoleIOTest {
public static void main(String[] args){

System.out.println("This program is designed to calaculate how many boxes of Oaties breakfast");
System.out.println("cereal are contained in one metric ton and the weight of one box of Oaties in");
System.out.println("metric tons. It will also calculate the number of whole cases and extra boxes");
System.out.println("of Oaties in one metric ton.");
System.out.println();
System.out.println("All of this will be done using the weight you supply for one box of Oaties.");
System.out.println();
System.out.println();


double x;
x= 35273.92;

double weight;
===== Here I need to know how to ask them to enter another number if they inter a negative. As it is now it tells them to inter a positive number but it continues on with the calculations. I want it to keep asking them until they enter a positive number or until they have entered 5 or so wrong numbers. If they enter 5 or so wrong numbers I need the program to quit.
System.out.println("Please enter the weight of one box of Oaties ceral in ounces.");
System.out.println("eg.(2, 5.05, 67.2, etc):");
System.out.println();
weight= JLiveRead.readLineDouble();
System.out.println();

if (weight<0){
System.out.println("Please enter a positive number.");
}
=====
===== Here I need to know how to round up to the nearest whole number. I have no clue how I would even start to do that. This would also help on my next problem.
System.out.println("You entered " + weight + " ounces for the weight of one box of Oaties");
System.out.println();
System.out.println("There are " + x/weight
+ " boxes of Oaties in one metric ton.");
System.out.println();
System.out.println("One box of Oaties weighs " + weight/x
+ " metric tons.");
int boxes;
===== Here I need to know how show how many whole cases + extra boxes are in a metric ton. All I have so far is how many cases are in a ton. :(
System.out.println();
System.out.println("Please enter the number of Oaties boxes in a case.");
System.out.println("eg.(2, 5, 67, etc):");
System.out.println();
boxes= JLiveRead.readLineInt();
System.out.println();

System.out.println("You entered " + boxes + " boxes of Oaties in one case.");
System.out.println();
System.out.println("There are " + x/(boxes*weight)
+ " cases of Oaties in one metric ton.");
=====


}
}
 

FeathersMcGraw

Diamond Member
Oct 17, 2001
4,041
1
0
Originally posted by: manly

Since String is immutable, this is horribly inefficient for any production code.

Although I've read that in recent versions of the 1.4 JVM, str1 + str2 + str3 actually compiles to something resembling StringBuffer tmp = new StringBuffer(str1); tmp.append(str2); tmp.append(str3);

But of course, relying on the compiler encourages naughty programming habits.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
1)

old
weight= JLiveRead.readLineDouble();
System.out.println();

if (weight<0){
System.out.println("Please enter a positive number.");
}


new
double weight = -1; //change up at the top


while(weight < 0)
{

if(weight < 0)
{
System.out.println("Please enter a positive number.");
weight= JLiveRead.readLineDouble();
}

if(wieght >= 0)
break;

}




2) Rounding is a good begginer programmer problem, i don't want to take the fun out of it but a hint would be to use modulo and division.

3) Your solution to #2 should solve this.
 

SithSolo1

Diamond Member
Mar 19, 2001
7,740
11
81
new
double weight = -1; //change up at the top


while(weight < 0)
{

if(weight < 0)
{
System.out.println("Please enter a positive number.");
weight= JLiveRead.readLineDouble();
}

if(wieght >= 0)
break;

}

I got 2 and 3 but the above does not work, it gets to if(weight >= 0) and it says weight is undefined.


public class ConsoleIOTest {
public static void main(String[] args){

System.out.println("This program is designed to calaculate how many boxes of Oaties breakfast");
System.out.println("cereal are contained in one metric ton and the weight of one box of Oaties in");
System.out.println("metric tons. It will also calculate the number of whole cases and extra boxes");
System.out.println("of Oaties in one metric ton.");
System.out.println();
System.out.println("All of this will be done using the weight you supply for one box of Oaties.");
System.out.println();
System.out.println();


double x;
x= 35273.92;

double weight=-1 ;

System.out.println("Please enter the weight of one box of Oaties ceral in ounces.");
System.out.println("eg.(2, 5.05, 67.2, etc):");
System.out.println();
weight= JLiveRead.readLineDouble();
System.out.println();

while(weight < 0)
{

if(weight < 0)
{
System.out.println("Please enter a positive number.");
weight= JLiveRead.readLineDouble();
}

if(wieght >= 0)
break;

}



System.out.println("You said there are " + weight + " ounces in one box of Oaties");
System.out.println();

double boxesy= x/weight;
int boxesz= (int) boxesy;

System.out.println("There are " + boxesz+1 + " boxes of Oaties in one metric ton.");
System.out.println();
System.out.println("One box of Oaties weighs " + weight/x + " metric tons.");
System.out.println();
System.out.println("Please enter the number of Oaties boxes in a case.");
System.out.println("eg.(2, 5, 67, etc):");
System.out.println();

double y= JLiveRead.readLineDouble();

System.out.println("You said there are " + y + " boxes of Oaties in one case.");
System.out.println();

double caseweight= weight*y;
double cases1= x/caseweight;
int cases2= (int) cases1;
double boxes1= x-(cases2*caseweight);
double boxes2= boxes1/weight;
int boxes3= (int) boxes2;
int boxes4= (boxes3+1);

System.out.println();
System.out.println("There are " + cases2 + " cases and " + boxes4 + " boxes of Oaties in one metric ton.");
System.out.println();
System.out.println("Thank You for using this program. =)");


}
}
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
rolleye.gif
im a poor speller and you are not very observent.
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
Originally posted by: BDawg
Originally posted by: manly
Originally posted by: BDawg

I've never experienced this problem...then again, I always combine all of my strings into one strings, put in my own breaks and then use the System.out.print, so I'd be something like this

String String1 = "";
String1 += "this is ";
String1 += "the first line";
String1 += "\n";
String1 += "this is ";
String1 += "the second line";
System.out.print String1;

That way, I'm in charge ofall of the formatting.
Since String is immutable, this is horribly inefficient for any production code.

How would you recommend doing this? I've never had any performance issues. Then again, I've never written any enterprise level code.

Don't use String class. Use the string buffer class.

Stringbuffer SB1 = new StringBuffer(100); // any number big enough to hold anything you want.
SB1.append("this is ");
SB1.append("the first line");
SB1.append("\n");
SB1.append("this is ");
SB1.append("the second line");
System.out.print(SB1);

You can convert to a string to if you want...
String String1 = new String(SB1);

This is far more effecient with StringBuffer is a mutable class. How much difference this will make depends on the code. A half dozen uses of the concatenation operators (+ and +=) aren't all that significant.
But if you have in a loop body that's making a lot of uses of them, then it can make some difference.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: SithSolo1
shhhhhhh

Why do I need to set double weight= -1?
Just curious

because you want it to enter the while loop the very first time, you could use a do-while loop if you want but the logic would be different.
 

manly

Lifer
Jan 25, 2000
13,566
4,233
136
Originally posted by: Noriaki

Don't use String class. Use the string buffer class.

Stringbuffer SB1 = new StringBuffer(100); // any number big enough to hold anything you want.
SB1.append("this is ");
SB1.append("the first line");
SB1.append("\n");
SB1.append("this is ");
SB1.append("the second line");
System.out.print(SB1);

You can convert to a string to if you want...
String String1 = new String(SB1);

This is far more effecient with StringBuffer is a mutable class. How much difference this will make depends on the code. A half dozen uses of the concatenation operators (+ and +=) aren't all that significant.
But if you have in a loop body that's making a lot of uses of them, then it can make some difference.
Actually, you're mistaken here. If all you're doing is concatenating constant strings together, then doing it all in one statement with the + operator is better because the compiler optimizes away the concatenations. This is part of the language spec.

If you introduce some variable(s) into the string building, then your optimization doesn't have a huge impact either. The only difference between your way and

String s = "abcde" + someVar + "fghij" + anotherVar;

is that you've allocated a StringBuffer with a specific capacity tuned for your usage (the default capacity is 16). By doing so, you can avoid extra work being done if the internal buffer needs to be grown.

FeathersMcgraw, I'm pretty sure the optimization I refer to is part of the language spec for some time now. I'm not aware what specific optimizations were made specifically in JVM 1.4 with respect to string handling. But what you suggested has always been part of Java, I believe. If you're concatenating Strings in one statement, only one StringBuffer is used. I.e. the code line I showed above is actually:

String s = (new StringBuffer()).append("abcde").append(someVar).append("fghij").append(anotherVar).toString();

Incidentally, if all you're doing is emitting some strings to the console, directly instructing the PrintStream to do so with individual strings might even be faster than building up your own large String and then calling print. I'd have to double check my optimizations book to see what the numbers looked like (circa JVM 1.2).
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: manly
Originally posted by: Noriaki

Don't use String class. Use the string buffer class.

Stringbuffer SB1 = new StringBuffer(100); // any number big enough to hold anything you want.
SB1.append("this is ");
SB1.append("the first line");
SB1.append("\n");
SB1.append("this is ");
SB1.append("the second line");
System.out.print(SB1);

You can convert to a string to if you want...
String String1 = new String(SB1);

This is far more effecient with StringBuffer is a mutable class. How much difference this will make depends on the code. A half dozen uses of the concatenation operators (+ and +=) aren't all that significant.
But if you have in a loop body that's making a lot of uses of them, then it can make some difference.
Actually, you're mistaken here. If all you're doing is concatenating constant strings together, then doing it all in one statement with the + operator is better because the compiler optimizes away the concatenations. This is part of the language spec.

If you introduce some variable(s) into the string building, then your optimization doesn't have a huge impact either. The only difference between your way and

String s = "abcde" + someVar + "fghij" + anotherVar;

is that you've allocated a StringBuffer with a specific capacity tuned for your usage (the default capacity is 16). By doing so, you can avoid extra work being done if the internal buffer needs to be grown.

FeathersMcgraw, I'm pretty sure the optimization I refer to is part of the language spec for some time now. I'm not aware what specific optimizations were made specifically in JVM 1.4 with respect to string handling. But what you suggested has always been part of Java, I believe. If you're concatenating Strings in one statement, only one StringBuffer is used. I.e. the code line I showed above is actually:

String s = (new StringBuffer()).append("abcde").append(someVar).append("fghij").append(anotherVar);

Incidentally, if all you're doing is emitting some strings to the console, directly instructing the PrintStream to do so with individual strings might even be faster than building up your own large String and then calling print. I'd have to double check my optimizations book to see what the numbers looked like (circa JVM 1.2).



you guys are all crazy!


just do:

String s = new String("This is the firstline\nThis is the second line.");

 

manly

Lifer
Jan 25, 2000
13,566
4,233
136
Originally posted by: Ameesh
[
you guys are all crazy!


just do:

String s = new String("This is the firstline\nThis is the second line.");
What are you talking about?

Calling the String constructor directly like this is less memory-efficient because the literal string isn't interned at compile-time. In other words, when you call the String constructor directly, a char array is instantiated and your character string is copied into that buffer.

If you just use String literals instead, then the runtime just points your reference s to a pre-existing shared String object from a central pool.