Beginner, this will be a quick fix, so please check it out!!

  • Thread starter the_transcriber
  • Start date
T

the_transcriber

Hi, extreme noob here (less than one week of Java exerience, or any
programming exp)

This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4

but it doesn't ask that question, it just answers without asking! any
help... ? this is from the Beginner Programming with Java for Dummies
- 2nd Edition. I typed it straight out of the book, and checked it
vary carefully. the output im currently gettins is... "Each kid gets 6
gumballs"



import java.util.Scanner;

class KeepingMoreKidsQuiet {

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int gumballs;
int kids;
int gumballsPerKid;

System.out.print("How many gumballs? How many kids? ");

gumballs = myScanner.nextInt();
kids = myScanner.nextInt();

gumballsPerKid = gumballs / kids;

System.out.print("Each kid gets ");
System.out.print(gumballsPerKid);
System.out.println(" gumballs.");
}
}
 
G

Gordon Beaton

This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4

but it doesn't ask that question, it just answers without asking!

Try using System.out.println() to ask the questions, instead of
System.out.print().

/gordon

--
 
C

Chris ( Val )

Hi, extreme noob here (less than one week of Java exerience, or any
programming exp)

This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4

but it doesn't ask that question, it just answers without asking! any
help... ? this is from the Beginner Programming with Java for Dummies
- 2nd Edition. I typed it straight out of the book, and checked it
vary carefully. the output im currently gettins is... "Each kid gets 6
gumballs"

import java.util.Scanner;

class KeepingMoreKidsQuiet {

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int gumballs;
int kids;
int gumballsPerKid;

System.out.print("How many gumballs? How many kids? ");

[snip]

You could use the 'println()' method instead.

Alternatively, you can add a new line to your string in the 'print()'
method, i.e: System.out.print("How many gumballs? How many kids?\n");

However, and more precicely, I would rather flush the stream
explicitly,
right after the 'print()' method call. This has the advantage of
pacing
your cursore for entry on the same line, which you would probably
like :)

System.out.flush();

HTH,
Chris
 
V

venu

Hi, extreme noob here (less than one week of Java exerience, or any
programming exp)

This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4

but it doesn't ask that question, it just answers without asking! any
help... ? this is from the Beginner Programming with Java for Dummies
- 2nd Edition. I typed it straight out of the book, and checked it
vary carefully. the output im currently gettins is... "Each kid gets 6
gumballs"

import java.util.Scanner;

class KeepingMoreKidsQuiet {

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int gumballs;
int kids;
int gumballsPerKid;

System.out.print("How many gumballs? How many kids? ");

gumballs = myScanner.nextInt();
kids = myScanner.nextInt();

gumballsPerKid = gumballs / kids;

System.out.print("Each kid gets ");
System.out.print(gumballsPerKid);
System.out.println(" gumballs.");
}



}- Hide quoted text -

- Show quoted text -

try using System.out.println(" xxxxx ");
the ' println()' takes the control to the next line, whereas print()
keeps it on the same line. Please initialize all variables to 0 in a
Java program. Well the last part is just my superstition.
 
T

the_transcriber

Hi, extreme noob here (less than one week of Java exerience, or any
programming exp)
This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4
but it doesn't ask that question, it just answers without asking! any
help... ? this is from the Beginner Programming with Java for Dummies
- 2nd Edition. I typed it straight out of the book, and checked it
vary carefully. the output im currently gettins is... "Each kid gets 6
gumballs"
import java.util.Scanner;
class KeepingMoreKidsQuiet {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int gumballs;
int kids;
int gumballsPerKid;
System.out.print("How many gumballs? How many kids? ");
gumballs = myScanner.nextInt();
kids = myScanner.nextInt();
gumballsPerKid = gumballs / kids;
System.out.print("Each kid gets ");
System.out.print(gumballsPerKid);
System.out.println(" gumballs.");
}
}- Hide quoted text -
- Show quoted text -

try using System.out.println(" xxxxx ");
the ' println()' takes the control to the next line, whereas print()
keeps it on the same line. Please initialize all variables to 0 in a
Java program. Well the last part is just my superstition.




Cool thanks I'll try it. Also, im so extremely noob, that i don't know
exactly what you mean by init all variables to 0? Can you give me an
example from my code? thanks
 
L

Lew

venu said:
Please initialize all variables to 0 in a Java program.
Well the last part is just my superstition.

Yes, it is, and not a good one.

The better rule is to initialize variables to their initial value. If that
happens to be 0 (or equivalent), good (albeit completely unnecessary to state
explicitly for instance or class variables). If it isn't 0 (or equivalent),
then why initialize to a different value from its initial value?

In the case of the OP:
better would have been to initialize to the initial value (which is only 0 if
gumballs's initial value is 0 and kids's isn't):

System.out.print("How many gumballs? How many kids? ");

int gumballs = myScanner.nextInt();
int kids = myScanner.nextInt();
int gumballsPerKid = (kids == 0? 0 : gumballs / kids);
 
T

the_transcriber

Hi, extreme noob here (less than one week of Java exerience, or any
programming exp)
This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4
but it doesn't ask that question, it just answers without asking! any
help... ? this is from the Beginner Programming with Java for Dummies
- 2nd Edition. I typed it straight out of the book, and checked it
vary carefully. the output im currently gettins is... "Each kid gets 6
gumballs"
import java.util.Scanner;
class KeepingMoreKidsQuiet {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int gumballs;
int kids;
int gumballsPerKid;
System.out.print("How many gumballs? How many kids? ");

[snip]

You could use the 'println()' method instead.

Alternatively, you can add a new line to your string in the 'print()'
method, i.e: System.out.print("How many gumballs? How many kids?\n");

However, and more precicely, I would rather flush the stream
explicitly,
right after the 'print()' method call. This has the advantage of
pacing
your cursore for entry on the same line, which you would probably
like :)

System.out.flush();

HTH,
Chris


Thanks, just a couple questions. What does "flush" refer to, or mean?
and also, what does the \n mean in the quote? Thanks for your help!
 
D

Daniel Pitts

Hi, extreme noob here (less than one week of Java exerience, or any
programming exp)
This program i am writing from a book is suppose to ask the question
"How many gumballs? How mand kids? " , and then allow the user to
input the 2 answers. for example : 80 4
but it doesn't ask that question, it just answers without asking! any
help... ? this is from the Beginner Programming with Java for Dummies
- 2nd Edition. I typed it straight out of the book, and checked it
vary carefully. the output im currently gettins is... "Each kid gets 6
gumballs"
import java.util.Scanner;
class KeepingMoreKidsQuiet {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int gumballs;
int kids;
int gumballsPerKid;
System.out.print("How many gumballs? How many kids? ");
[snip]

You could use the 'println()' method instead.

Alternatively, you can add a new line to your string in the 'print()'
method, i.e: System.out.print("How many gumballs? How many kids?\n");

However, and more precicely, I would rather flush the stream
explicitly,
right after the 'print()' method call. This has the advantage of
pacing
your cursore for entry on the same line, which you would probably
like :)

System.out.flush();

HTH,
Chris


Thanks, just a couple questions. What does "flush" refer to, or mean?
and also, what does the \n mean in the quote? Thanks for your help!

You'll find a lot of things will "buffer" data. A buffer is usually a
place where its fast to add/remove data to before it gets "sent"
somewhere else. For instance, System.out.print will buffer somewhere,
because its faster than writing a whole lot of small messages to the
screen in a lot of cases. Flush tells the "buffer" to send everything
its accumulated.

Some things flush automatically, and some things don't have a buffer.
System.out.println will flush automatically, including anything that was
buffered BEFORE the call.
System.out.print may buffer, and might not flush.

Now, the next important piece of information: Just because something is
buffered, don't expect that none of your output will happen. Buffers
tend to be of limited size, and will flush themselves before/as they
fill up. If this happens, you might send part of your message before
the rest of it. Especially if you don't flush.

HTH,
Daniel.
 
D

Daniel Pitts

venu wrote:
[snip]
Please initialize all variables to 0 in a
Java program. Well the last part is just my superstition.
Please do NOT initialize all variables to 0 in a Java program!

You should not declare your variables until they are first needed and
can be assigned. If possible, its good practice to declare them final.
This ensures that they will only ever be initialized once, even if you
go through different branches to initialize them.

final int pageSize;
if (pageNumber == 0) {
pageSize = 10;
} else {
pageSize = 20;
}
someOtherObject.doSomethingWithPageSize(pageSize);
Better yet, if you can initialize a variable to two different values,
but only use one, make it into a method:

private int getPageSize() {
if (pageNumber == 0) {
return 10;
} else {
return 20;
}
}

// then you can use:
final int pageSize = getPageSize();
someOtherObject.doSomethingWithPageSize(pageSize);

Or even better, "inline" that variable (don't use the variable, just use
what you assigned to it, since it has a meaningful name now)

someOtherObject.doSomethingWithPageSize(getPageSize());

Even if you call getPageSize a few times, its better design. If and
only if you find that calling that method slows down your application
beyond a reasonable level should you "optimize" to saving the value.
 
L

Lew

Daniel said:
Please do NOT initialize all variables to 0 in a Java program!

You should not declare your variables until they are first needed and
can be assigned. If possible, its good practice to declare them final.
This ensures that they will only ever be initialized once, even if you
go through different branches to initialize them. ....
Or even better, "inline" that variable (don't use the variable, just use
what you assigned to it, since it has a meaningful name now)

someOtherObject.doSomethingWithPageSize(getPageSize());

Daniel's excellent advice translated to the example:

boolean terminated;
do
{
System.out.print("How many gumballs? How many kids? ");
System.out.flush();

final int gumballs = myScanner.nextInt();
final int kids = myScanner.nextInt();
final int gumballsPerKid = (kids == 0? 0 : gumballs / kids);

System.out.println( "" + gumballs +" gumballs for "+
kids +" kids = "+
gumballsPerKid +" gumballs per kid." );
System.out.print( "Continue? " );
System.out.flush();

final String cont = myScanner.next();
terminated = (cont.length() == 0
|| Character.toLowerCase( cont.charAt(0) ) != 'y' );
} while ( ! terminated );

Naturally there are several ways to recast this code snippet. This one is
just to tie together advice from this thread about flush() and declaration and
initialization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,722
Latest member
NelsonHeil

Latest Threads

Top