Search code examples
javavariablesjava.util.scannermain-method

I want to use main variable(Scanner) in another method


import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
class Graph_mod extends Frame {
public static void main( String[] args ) {
    Scanner input = new Scanner(System.in);
    System.out.println("enter v :");
    double v = input.nextDouble();
    System.out.println("enter a :");
    double a1 = input.nextDouble();
    double a2 = a1*Math.PI/180;
    double t = v*Math.sin(a2)/9.8;
    double h = Math.pow(v*Math.sin(a2), 2)/(2*9.8);

I want to use this variable(h, r)

    double r = Math.pow(v,2)*Math.sin(2*a2)/9.8;
    System.out.println("t is" + t );
    System.out.println("h is" + h );
    System.out.println("r is " + r );
new Graph_mod( "graph");
}
@Override
public void paint( Graphics g )
{
g.setColor( new Color( 0x000000 ) );
g.drawLine( 0, 300, 600, 300 );
g.drawLine( 300, 0, 300, 600 );

for( int i = 0; i < 6; i++ )
{
g.drawLine( i*100 , 298 , i*100, 302 );
g.drawLine( 298, i*100, 302, i*100 );
}


g.setColor( new Color( 0xFF0000 ) );
int px=-1;
int py=300;
int x, y;
for( x = 0; x < 600; x++ )
{
y = My_function( x );
g.drawLine( px, py,x, y );
px = x;
py = y;
}
}

private int My_function( int inX )
{
double x, y;
int outY;
x = (inX - 300.)/ 100.;

In here

y = (-4*h/Math.pow(r,2) * x * x) + (4*h/r*x);
outY = (int)( y * -100. + 300. );
return outY;
}

public Graph_mod( String title )
{
super( title );

addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent we ){
System.exit( 0 );
}
} );

setBounds( 0, 0, 600, 600 );
setVisible( true );
}
}

I want to make a parabola movement calculator code, but in the process that making a graph, I had some problem. I imported scanner and used in main method, but I can't use it in another method(My_funtion). I know it's get better when I use parameter, but it's too difficult to me... please help


Solution

  • Declare variables h and r as class global, for example:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Scanner;
    
    class Graph_mod extends Frame {
        private static double h = 0.0;   // Place variable here ***
        private static double r = 0.0;   // Place variable here ***
    
    public static void main( String[] args ) {
        //..........................
        //..........................
        //..........................
        h = Math.pow(v*Math.sin(a2), 2)/(2*9.8);
        r = Math.pow(v,2)*Math.sin(2*a2)/9.8;
        //..........................
        //..........................
    }
    

    However, because these variables are used within the main() method they must be declared as static since the main() method itself is static. It will however be okay to use in other non-static class methods even if the variables are declared as static.

    If it is in fact the Scanner object itself you want to use across some or all class methods then declare that as class global, for example:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Scanner;
    
    class Graph_mod extends Frame {
        Scanner input = new Scanner(System.in);  // Place scanner here ***
        private static double h = 0.0;
        private static double r = 0.0;
    
    public static void main( String[] args ) {
        System.out.println("enter v :");
        double v = input.nextDouble();
        System.out.println("enter a :");
        double a1 = input.nextDouble();
        double a2 = a1*Math.PI/180;
        double t = v*Math.sin(a2)/9.8;
        h = Math.pow(v*Math.sin(a2), 2)/(2*9.8);
        r = Math.pow(v,2)*Math.sin(2*a2)/9.8;
        //..........................
        //..........................
    }
    

    Now you can use input.nextDouble() method in any method that is declared within the class where the Scanner object is globally declared but I don't think this is what you really want. I think what you're truly asking for is the access to specific variables such as h and r which were filled within the main() method.