Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
With all the Java that I have learned in the 123 pages of "Java for Dummies" so far I feel like a 2 year old kid going through the "Why" stages of their life. So I will post some code that I have a couple of questions about. This was not an excersice from the book but I took different parts from what I understood and wrote my own code. This code add to different dollar amounts and add in good old California sales tax. The code works fine but I want to understand "Why" it is doing somethings instead of copying from the book.

import java.util.Scanner;

public class numbers
{

static Scanner b = new Scanner(System.in);

static public void main(String[] args)
{


System.out.print("Pick an amount ");
double r = b.nextDouble();

System.out.print("Now pick an amount to add ");
double j = b.nextDouble();


System.out.println("With Tax the amount is " + (r + j) * 1.075);
}
}

Now my first question. I created a Scanner class so I could receive input from the user. Now the 'new Scanner(System.in); is being assigned to a variable b' if I understand that right.

Now in the code "Pick an amount" the "b.nextDouble()' takes the the users input and assings that to 'b' which I assign to 'double r'. I also use that same code in the "Now pick an amount to add" "b.nextDouble();".

Since I already assinged the USERS input to 'b' from the first entry I thought that it would need another letter assined to it. Meanning that I would have to code it so it would create 2 different Scanner like this.

static Scanner b = new Scanner(System.in);
static Scanner c = new Scanner(System.in);

So in the code further down it would look like this.

System.out.print("Pick an amount ");
double r = b.nextDouble();

System.out.print("Now pick an amount to add ");
double j = c.nextDouble();

So, "Why" can I assign a variable to 'b' and then assign another value to the same variable, in this case 'b' and have it still work? Does 'b' pass it on to 'r' and then later in the code 'b' passes it on to 'j'

Thanks for the help, I hope I haven't confused you.

-Lars
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
System.in is just reading in from the keyboard, the b.nextDouble() bit is just reading the next double from the stream of data coming from the keyboard, the b just creates an instance, though in this case it would work better as a static, in this case it would be simpler for you to understand if it was just Scanner.nextDouble(). As you can use scanners to scan files or strings of text when having an instance is more useful, but that wouldn't work for other uses of the scanner class. I think this is just something you need to accept, like why 2+3=5.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
So I would need to re-write the line 'static Scanner b = new Scanner(System.in); Since b is the variable of TYPE Scanner and just write instead 'new Scanner(System in);' and later in the code write 'Scanner.nextDouble();' would something like that get rid of the need for the Scanner variable?

So the 'b.nextDouble' just transfers what I type into the 'double r' variable I created and does not remember anything.


Can I just write 'new Scanner(Syetem.in); intstead of what I wrote to simplify the code?

-Lars
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
larswik said:
So I would need to re-write the line 'static Scanner b = new Scanner(System.in); Since b is the variable of TYPE Scanner and just write instead 'new Scanner(System in);' and later in the code write 'Scanner.nextDouble();' would something like that get rid of the need for the Scanner variable?

So the 'b.nextDouble' just transfers what I type into the 'double r' variable I created and does not remember anything.


Can I just write 'new Scanner(Syetem.in); intstead of what I wrote to simplify the code?

-Lars
No, I have clearly confused you, in reality it isn't possible to use Scanner as a static, but it would make more sense to do so in this case.
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
yeah scanner can be a bit though if you aren't familiar with java, when I first studied java we were though using Keyboard, which was a class made by the course administrator (if memory serves) - to understand how it worked we spent more than a few lessons studying implications of System input - some understood quickly, some not at all, I suppose- Scanner isn't very different from any other class calls in java - so it's important you understand.

When you declare a new Class (in this case: Scanner) you create a class that runs "out of sight" of your main - all the things it does is to simplify the main method structure.

This is probably confusing, but this is your code used in another sense (and not Scanner, but a self made Class)

Code:
Class mainClass {
   public static void main(String[] args) {
       moo cow = new moo();
       cow.graze();
       cow.graze();
       cow.budgie();
    }
}

class moo {
    public void graze() {
       System.out.println("a cow grazes");
    }
    public void budgie() {
       System.out.println("a cow is not a budgie");
    }
}

only difference is that System.in and Scanner is code you don't know, but know how they work (they give some data, data makes application happy, increases coder happiness - yay! :) )


edit: don't focus on "public void" as much, if you do - I'm just trying to explain in this case and "public void" means that the method using this won't have to return any kind of data - if I would use "public int" I would have to use a "return" statement, which sends an integer back to wherever the method was called. (so no, don't care about it just now)
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I know the Scanner class is out of site like you said. It is used in this case to allow input from the keyboard. But in the original code, after importing Scanner Class, I created a new Scanner using this line of code "static Scanner b = new Scanner(System.in);". Now am I wrong in thinking that 'b' is the variable or a path of transfering the input from the keyboard to the variable in the line "double r" When I worked a little with Lingo that 'r' was refered to as a 'Container' since it was holding information to be used later which is also known as a variable, right?

Later in the code I write this "double r = b.nextDouble();" From what I have slight understanding of is that the 'nextDouble();' line is what recieves the input from the keyboard, is that right? and the 'b' in front of 'b.nextdouble();' is refering to the "Scanner b = new Scanner(System.in); from that point the keyboard input is being transfered to the "double r" and held there for further use?

I just want to make sure that my understanding of that so far is right. It does me no good to plow through the book without understanding what I am reading.

thanks again,

-Lars
 

Compile 'em all

macrumors 601
Apr 6, 2005
4,131
359
larswik said:
Later in the code I write this "double r = b.nextDouble();" From what I have slight understanding of is that the 'nextDouble();' line is what recieves the input from the keyboard, is that right? and the 'b' in front of 'b.nextdouble();' is refering to the "Scanner b = new Scanner(System.in); from that point the keyboard input is being transfered to the "double r" and held there for further use?

No. There is no such thing as "path of transfering". Once something is entered
through the keyboard, it is assigned to b, according to statement
Scanner b = new Scanner(System.in);.

The following statement, double r = b.nextDouble(); scans b (i.e the input
_already_ entered) looking for a double.
 

MacNeXT

macrumors 6502
Jun 21, 2004
258
0
Compile 'em all said:
No. There is no such thing as "path of transfering". Once something is entered
through the keyboard, it is assigned to b, according to statement
Scanner b = new Scanner(System.in);.

The following statement, double r = b.nextDouble(); scans b (i.e the input
_already_ entered) looking for a double.

I can't make any sense out of this! Nothing is assigned to b except for a scanner object (created by new Scanner(...) ). b now refers to an object of class Scanner. Certainly no keyboard input is assigned to b! I think the most important thing to understand when learning Java is getting these concepts right!

b.nextDouble(); is a call to a method of the object assigned to b. It returns, in this case, the next double value read from system.in (user input).

larswik said:
Later in the code I write this "double r = b.nextDouble();" From what I have slight understanding of is that the 'nextDouble();' line is what recieves the input from the keyboard, is that right? and the 'b' in front of 'b.nextdouble();' is refering to the "Scanner b = new Scanner(System.in); from that point the keyboard input is being transfered to the "double r" and held there for further use?

About right but the way you explain it is a bit off. Again, b refers to an object. You instantiated that object at the line "Scanner b = new Scanner(System.in);". nextdouble() is a method of an object of the Scanner class. This means you can call nextdouble() on b : b.nextDouble(). This particular method returns a double value, which you assign to the variable r in your code. The double value it returns is the next value the scanner can read from system.in, or to put it simply: "the keyboard".

Again, it's very important to fully understand concepts such as object, class, instantiation, methods, return values of methods, variables, primitive variable types (int, double ...) etc. etc.
 

Thom_Edwards

macrumors regular
Apr 11, 2003
240
0
MacNext explained it very well. i'll try again here so maybe hearing it in slightly different ways may help understanding.

the way you are using Scanner is a good example of encapsulation. b is a Scanner that can perform an action called nextDouble(). just like a dog can fetch(). nextDouble() returns input immediately and then is finished without having any recollection of what it just did; no storage, changing, etc. your dog brings back the bone and drops it on the ground for you instead of holding it in her mouth.

and since netDouble returns a value, something has to be there to accept the return value. (your dog doesn't fetch() by just running around in circles forever with a bone in her mouth. she returns it to you.) you use = to do that, like in your code r = b.nextDouble(). you then use j to store the next value. r and j can only hold one double each. that is why you have separate doubles.

you only need one Scanner to accept the two, but you need two variables (doubles in this case) to store each. also, you would only need one dog to fetch two different bones, but (for sake of argument) you need two holes to put them in.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
The dog fetching the bone, now that is laungage that I understand : ) so my basic understanding of the process is somewhat on target.

I agree with you MacNeXT understanding of the basics is important to know. The Java for Dummies book talked about that in chapter one but never coding much before it was a little greek to me. But now as I get into the book and see how the code works I get a better understanding of it. I think I should go back and read chapter one again now that I have little better understanding.

I know and can see how the scanner class is working. But I get lost in the Terminology, ie. Method, instances, assignments and so on.

Thanks for the help. I think a couple different versions helped explain it. It's one thing coping the stuff from the book and seeing it work to one day writing my one code. Right now it is monkey see monkey do. I need to get to monkey understand.

Thanks,

-Lars
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.