Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Alternative to the scanner method

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Alternative to the scanner method

     
    import java.util.*;
    import javax.swing.*;
     
    public class CH4Q16 {
     
     
        public static void main(String[] args) {
     
            System.out.print("Enter The Double Number: ");
            Scanner x = new Scanner(System.in);
            double num=x.nextDouble(); 
            for(int i=1;i<16;i++) 
            {
                System.out.println(Math.pow(num, i)); 
            }
        }
     
    }

    This code takes a number (a double) from the user and prints it's squares all teh way to the 15th power. The code works but my question is to find an alternative to the "scanner x = new Scanner(System.in);" to get the number from the user.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Alternative to the scanner method

    There's the BufferedReader class, but if this is all you need to do I'd just use the Scanner class. May I ask why you don't want to use the Scanner class?

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Alternative to the scanner method

    Because I have not covered the scanner method in my course yet hence this is was not accepted by my teacher. I learned how to do it this way by reading tutorials online in the first place but I was limited to what I have covered only.

    Here's what I did after re-reading my book.

    import java.util.*;
    import javax.swing.*;
     
    public class CH4Q16 {
     
     
        public static void main(String[] args) {
     
        	Random myNums = new Random();
     
        	String value;
     
           value = JOptionPane.showInputDialog("Enter The Double Number: ");
            double  num = Double.parseDouble(value); 
            double num1=myNums.nextDouble(); 
            for(int i=1;i<16;i++) 
            {
                System.out.println(Math.pow(num, i)); 
            }
        }
     
    }
    Don't know why I didn't think of that earlier <.<

    Thanks for the quick reply though Hello ^^
    Hopefully when I improve I can help around here in the future but can't do much now as it's been only a month since I started Java

Similar Threads

  1. JNI Alternative?
    By janusmccarthy in forum Java Native Interface
    Replies: 1
    Last Post: November 14th, 2009, 12:22 PM
  2. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 10:44 AM
  3. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 09:49 PM
  4. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 10:01 PM
  5. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 18th, 2009, 12:07 AM