Results 1 to 5 of 5
  1. #1
    Zunon is offline Member
    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    Lightbulb how to make java listen to the input?

    i'm new to java nd while reading a book I found:
    Java Code:
    /* 
       Try This 2-1 
     
       Compute the distance to a lightening 
       strke whose sound takes 7.2 seconds 
       to reach you. 
    */     
    class Sound {     
      public static void main(String args[]) {     
        double dist; 
     
        dist = 7.2 * 1100; 
     
        System.out.println("The lightening is " + dist +  
                           " feet away.");  
        
      }     
    }
    but I made my own version of it that shows it in meters form :
    Java Code:
    /*
    	Try this 2-1
    	Compute the distance to a lightning
    	strike whos soind takes 7.2 seconds
    	to reach you
    */
    class Sound {
    	public static void main(String args[]) {
    		double dist, T;
                    T = 7.2; // Input or seconds
    		dist = (T * 1100) / 3.2808399;
    		
    		System.out.println("the lightning strike is " + dist + "M away");
    	}
    }
    However I don't want to keep it at 7.2 seconds, I want the user to input his own time value in the console ... so I want the input to be assigned to T.

    so ... how do you make java listen to the input? and then how do you make java use the input and assign it to T?

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: how to make java listen to the input?

    You can use the Scanner class to do this. I will let you figure out how to do this but just remember google is your best friend

  3. #3
    Zunon is offline Member
    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    Default Re: how to make java listen to the input?

    Quote Originally Posted by jhuber151 View Post
    You can use the Scanner class to do this. I will let you figure out how to do this but just remember google is your best friend
    sorry couldn't find an understandable explanation through google ... care to explain?

    I am a beginner after all

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: how to make java listen to the input?

    Here's a link to a tutorial on Scanning: Scanning tutorial.

    If you're still stuck, please tell us exactly what confuses you. We really shouldn't be expected to write another tutorial from scratch. Please help us understand how to help you.

  5. #5
    Zunon is offline Member
    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    Default Re: how to make java listen to the input?

    oh thank you now I know ...

    here's the final code
    Java Code:
    /*
        Try this 2-1
        Compute the distance to a lightning
        strike whos soind takes 7.2 seconds
        to reach you
    */
    import java.util.Scanner;
    class Sound {
        public static void main(String args[]) {
    	
            double dist;
    
    		Scanner scanner = new Scanner(System.in);
    
    		System.out.println("Please tell me, what was the delay from you seeing it then you hearing it?");
    		System.out.println();
    
    		double T = scanner.nextDouble();
            dist = (T * 1100) / 3.2808399;
    
    		System.out.println();
            System.out.println("the lightning strike is " + dist + "M away");
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 09-27-2011, 06:46 PM
  2. Replies: 3
    Last Post: 05-22-2011, 03:16 PM
  3. Make a menybutton (action)listen!
    By LennyKosmos in forum New To Java
    Replies: 1
    Last Post: 10-25-2010, 05:12 PM
  4. Replies: 7
    Last Post: 04-10-2010, 11:31 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •