Beginner Problem - system.out.print

K

KyoGaSuki

I just started taking a Programming With JAVA class and we have a lab
to do, and I just can't figure it out.

First, I am supposed to ask the user for a 4-digit number (I think
this is correct):

System.out.print("Please Enter A 4-Digit Integer: ");




Then we are supposed to output the digits onto separate lines:

ex: 4321 would look like
4
3
2
1



I know that to do this I can divide it first by 1000, then divide the
remainder by 100, then that remainder by 10...But my problem is
defining the variable (or the number that the user inputs). Can
anybody help? This isn't a graded assignment, but I just want to
understand what I am doing wrong. Please help.
 
R

rossum

I just started taking a Programming With JAVA class and we have a lab
to do, and I just can't figure it out.
You will probably be better off posting this in comp.java.lang.help
which is intended for basic questions like this one.

First, I am supposed to ask the user for a 4-digit number (I think
this is correct):

System.out.print("Please Enter A 4-Digit Integer: ");
It is correct as far as it goes, but you need to do more than that.

Then we are supposed to output the digits onto separate lines:

ex: 4321 would look like
4
3
2
1



I know that to do this I can divide it first by 1000, then divide the
remainder by 100, then that remainder by 10...
That should work.
But my problem is
defining the variable (or the number that the user inputs).
You are asking the user for a 4-digit integer. If you look at the
built in types that Java provides you will see that there is an
integer type called "int". That is the type you should use here.

int usersNumber;

will define the variable. Now you have to find a way to set that
number to what the user typed in:

Can
anybody help? This isn't a graded assignment, but I just want to
understand what I am doing wrong. Please help.
We can help you more if you post all of the code you have written so
far. This tells us a lot more about what you know and what you are
having problems with.

Have you had a look at the Sun Java tutorials?
http://java.sun.com/docs/books/tutorial/index.html

Some of them will be too advanced for you at the moment, but you
should be able to follow some of the more basic parts.

We will not do your homework for you, whether it is graded or not. We
will help you, but you need to show willing by posting your actual
code, no matter how bad and no matter if you can't even get it to
compile.

rossum
 
K

KyoGaSuki

Oh, sorry ^^;

Well, that was only my second java class (so this is going to be SO
embarassing..):


/**
* @(#)Digits.java
*
* Digits application
*
* @author
* @version 1.00 2008/1/17
*/

public class Digits {

public static void main(String[] args) {

// TODO, add your application code

int userNumber;

System.out.print("Enter a 4-digit integer: ");

}
}



I have absolutely no clue where to go after this...I know I am missing
something major in what I already have, though...I should be shot for
how little I understand this x.x
 
R

rossum

Oh, sorry ^^;

Well, that was only my second java class (so this is going to be SO
embarassing..):
Don't worry, we were all there once. When I first coded a goto I
misspelled it ("go to" vs "goto").

/**
* @(#)Digits.java
*
* Digits application
*
* @author
* @version 1.00 2008/1/17
*/

public class Digits {

public static void main(String[] args) {

// TODO, add your application code

int userNumber;

System.out.print("Enter a 4-digit integer: ");
At this point you have two things to do:

1 Get the actual number from the user - so far you have only asked
for it.

2 Print out the given digits in order.

As a minor point is is better to indent with spaces rather than tabs
when posting on usenet - different people have different tab settings
so tabbed code may display very strangely.
}
}



I have absolutely no clue where to go after this...I know I am missing
something major in what I already have, though...I should be shot for
how little I understand this x.x

You do not have to work on the two tasks in the order I gave. For
example you could work on task 2 first by putting in a dummy for task
1:

public static void main(String[] args) {

// 1 Get number from user
int userNumber;
System.out.print("Enter a 4-digit integer: ");
userNumber = 4321; // Dummy user number

// 2 Print number one digit per line
// TODO, add your application code for task 2
}

Doing that will allow you to progress.

Remember to test your work, does it work with numbers like "1203",
"0123" or "1230".


Now for task 1. If you look through the Java documentation, your Java
IDE might have an offline copy or else look at
http://java.sun.com/javase/6/docs/api/ you will see that there is a
class called java.util.Scanner. Have a read of the documentation for
that class. That will help you with task 1.

Be warned that there is a System.in to match the System.out you have
already been using; that does not do what you might think it does. It
works at a lower level than Scanner and is more difficult for a
beginner to use.

The Sun documentation is also available in Chinese or Japanese if that
is better for you, see http://java.sun.com/javase/reference/api.jsp
and look under "Core API Docs" for version 6.

rossum
 
L

Lew

KyoGaSuki said:
Oh, sorry ^^;

Well, that was only my second java class (so this is going to be SO
embarassing..):


/**
* @(#)Digits.java
*
* Digits application
*
* @author
* @version 1.00 2008/1/17
*/

public class Digits {

public static void main(String[] args) {

// TODO, add your application code

int userNumber;

System.out.print("Enter a 4-digit integer: ");

}
}



I have absolutely no clue where to go after this...I know I am missing
something major in what I already have, though...I should be shot for
how little I understand this x.x

The next step is to find a method or class that knows how to bring things in
from the outside, the way System.out knows how to put things out to the outside.

So next take a look at the Javadocs for System.out and see if they lead you to
related things.

In the "Field Summary" section of the System Javadocs
<http://java.sun.com/javase/6/docs/api/java/lang/System.html>
we see that 'out' is a 'java.io.PrintStream' object, and that there is,
indeed, a related field, 'in', which is a 'java.io.InputStream'.

<http://java.sun.com/javase/6/docs/api/java/io/InputStream.html>

So one possible way to bring in information is via one of the InputStream
methods, like read().

The problem is, this version of read() seems rather crude. You're going to
face a lot of work converting raw bytes into useful information. So what else
is there around the java.io package more generally?

<http://java.sun.com/javase/6/docs/api/java/io/package-frame.html>

Oy. There's a lot there. At this point you might find yourself turning to
the tutorials in desperation.

<http://java.sun.com/docs/books/tutorial/essential/io/index.html>

It looks like
<http://java.sun.com/docs/books/tutorial/essential/io/scanfor.html>
might hold some relief.

This tutorial chapter discusses the java.util.Scanner class
<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>
which is chock-full of helpful methods.

Essentially, you wrap a Scanner instance around an input like System.in and
play with it from there.

There are also various java.io.Reader classes that you might find useful.

Enjoy.
 
M

Mark Space

While reading the Javadoc and the tutorials is excellent advice, I think
that's pretty hard on someone who is brand new. It's unfortunate that
Java doesn't provide a nice input object for you like System.out is a
nice output object, but that's how it is.

After checking the tutorials, see if you can find this:

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));

That's what you need to make a nice reader. Now you need to get the
String that the user types, and then turn that String into an integer.

Here's a hint:
public class Digits {

public static void main(String[] args) {

// TODO, add your application code

int userNumber;

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a 4-digit integer: ");

String inputString = in.readline();

That makes the nice input reader for you, and calls the right method to
read the input. That's steps one and two above. Now you have to figure
out how to go from inputString to userNumber.

If you can find the two lines I added in the Javadocs and the tutorials
(see Lew's post), you should now have much better idea how to look for
that third step, converting a String to an integer.
 
K

KyoGaSuki

While reading the Javadoc and the tutorials is excellent advice, I think
that's pretty hard on someone who is brand new. It's unfortunate that
Java doesn't provide a nice input object for you like System.out is a
nice output object, but that's how it is.

After checking the tutorials, see if you can find this:

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));

That's what you need to make a nice reader. Now you need to get the
String that the user types, and then turn that String into an integer.

Here's a hint:
public class Digits {
public static void main(String[] args) {
// TODO, add your application code
int userNumber;

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter a 4-digit integer: ");

String inputString = in.readline();



That makes the nice input reader for you, and calls the right method to
read the input. That's steps one and two above. Now you have to figure
out how to go from inputString to userNumber.

If you can find the two lines I added in the Javadocs and the tutorials
(see Lew's post), you should now have much better idea how to look for
that third step, converting a String to an integer.

thank you SO much!
 
K

KyoGaSuki

While reading the Javadoc and the tutorials is excellent advice, I think
that's pretty hard on someone who is brand new. It's unfortunate that
Java doesn't provide a nice input object for you like System.out is a
nice output object, but that's how it is.
After checking the tutorials, see if you can find this:
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
That's what you need to make a nice reader. Now you need to get the
String that the user types, and then turn that String into an integer.
Here's a hint:
KyoGaSuki said:
public class Digits {
public static void main(String[] args) {
// TODO, add your application code
int userNumber;
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
String inputString = in.readline();

That makes the nice input reader for you, and calls the right method to
read the input. That's steps one and two above. Now you have to figure
out how to go from inputString to userNumber.
If you can find the two lines I added in the Javadocs and the tutorials
(see Lew's post), you should now have much better idea how to look for
that third step, converting a String to an integer.

thank you SO much!

Quick question for any of you familiar with JCreator. I have been
using JCreator LE for all of this so far (not sure if this is the
right group to post this in, but I'll try), and no matter what code I
put in (even example codes), it always comes up with errors saying
"cannot find symbol class ________" (in the blank is stuff like
"BufferedReader" and "InputStreamReader). Could it just be the
program?
 
L

Lew

KyoGaSuki said:
Quick question for any of you familiar with JCreator. I have been
using JCreator LE for all of this so far (not sure if this is the
right group to post this in, but I'll try), and no matter what code I
put in (even example codes), it always comes up with errors saying
"cannot find symbol class ________" (in the blank is stuff like
"BufferedReader" and "InputStreamReader). Could it just be the
program?

You probably are neither using the fully-qualified name (FQN) of the class

java.io.BufferedReader

nor importing the FQN at the top of your source file:

import java.io.BufferedReader;

To access classes from packages other than the one of the class itself you
must do one or the other.
 
M

Mark Space

KyoGaSuki said:
Quick question for any of you familiar with JCreator. I have been
using JCreator LE for all of this so far (not sure if this is the
right group to post this in, but I'll try), and no matter what code I
put in (even example codes), it always comes up with errors saying
"cannot find symbol class ________" (in the blank is stuff like
"BufferedReader" and "InputStreamReader). Could it just be the
program?

I'm not familiar with JCreator. I use NetBeans, which free and excellent.

In NetBeans you can Crtl-Shift-I and NetBeans will fix imports for you.
Also, if a line is flag with an error like this, it puts a yellow
caution symbol on the left of the line, which tells you you can press
alt-Enter and it'll pop-up a list of possible fixes. See if you can
find similar functions with JCreator.
 
K

KyoGaSuki

To everybody who helped me with this, thank you! here is the final
code I came up with (and it actually works):


/**
* @(#)digits.java
*
* digits application
*
* @author Kasumi
* @version 1.00 2008/1/28
*/
import java.util.Scanner;
public class digits {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Please enter a 4-digit number: ");
int num = input.nextInt();
System.out.println(num/1000);
int num2 = num%1000;
System.out.println(num2/100);
int num3 = num2%100;
System.out.println(num3/10);
int num4 = num3%10;
System.out.println(num4/1);
}
}
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top