Jump to content

The scanner ?! How 2 use

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
miromtm

miromtm

    Newbie

  • Members
  • PipPip
  • 18 posts
hello guys n happy new year , n never foregt what is happening in gazza :S

i needed some information about how to use the SCANNER since every time i use it it always don't work

public class NewClass {


    scanner s = new scanner (System.in);

    

    int x= s..nextInt();

    int y = s..nextInt();

    int sum = x+y;

    System.out.println("THE SUM IS "+ sum );

    

    

}


DOESNT COMPILE :s:s:s , n it says something like CLASS NOT FOUND

thx n waiting 4 ur HELP

#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Try to do like this next time :)

First import our fine scanner from java's own library, util!
import java.util.*;

Afterward declare the class name
public class NewClass {

Second after that our scanner !
static Scanner s = new Scanner(System.in);
static represents it to be able use different values in different times however you will not be able to change the same value since it will be declared for every the same as you did from start.(Someone else tell him/her in a better way please :>)

Third the main !
public static void main(String[] arg) {

The program itself !
System.out.println("First value");
		int X = s.nextInt();
		
		System.out.println("Second value");
		int Y = s.nextInt();
		
		int sum = X+Y;
		
		System.out.println("First value + second value equals "+sum);
            }
}

So the whole code will look like this !
import java.util.Scanner;
public class Test {
	static Scanner s = new Scanner(System.in);
public static void main(String[] arg) {
		
		System.out.println("First value");
		int X = s.nextInt();
		
		System.out.println("Second value");
		int Y = s.nextInt();
		
		int sum = X+Y;
		
		System.out.println("First value + second value equals "+sum);
	}
}
Enjoy :)
Posted Image

#3
miromtm

miromtm

    Newbie

  • Members
  • PipPip
  • 18 posts
man WAT A GR8 EXPLANATION am so thankful , but what is the different between

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

and
scanner s = new scanner (System.in);


#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
scanner s = new scanner (System.in);

Gives an error, as "scanner" isn't a class. lol It should be:

Scanner s = new Scanner(System.in);

Also static just means that it doesn't belong to a particular object. :)

#5
miromtm

miromtm

    Newbie

  • Members
  • PipPip
  • 18 posts
LOL, that is because i didn't import it in da first place lol

n thx man 4 making things clear ...

#6
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Good thing everything is good now :)
Posted Image