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 2 of 2

Thread: Simple I/O Question...well could be simple for you?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple I/O Question...well could be simple for you?

    Okay guys so say i wanted to take in code from a scanner object for an example
    i do something like this

     System.out.println("EXPRESSION MIN_X MAX_X MIN_Y MAX_Y DELTA_X WIDTH HEIGHT");

    Where i want the user to enter in something as the following "((x^2)-50)" -10 +10 -100 +100 0.01 800 500


    now would there be anyway of optimizing my code so that i dont have to keep doing something like this:

     Scanner inScan = new Scanner (System.in);
      System.out.println("Enter Expression:");
      String expression = inScan.nextLine();
      System.out.println("Enter MIN_X");
      int Min_X = inScan.nextInt();

    and just simply extract code from one scanner call?

    Do you understand what i mean?

    Any help is appreciated!


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Simple I/O Question...well could be simple for you?

    What's the difference? The user ends up with the same amount of typing. If it's all on one line, they have to separate the fields with horizontal whitespace (spaces), if it's question-by-question, they have to type vertical whitespace (enter key). With the enter key, they also get to read reminders of what they should be typing. You can also ask users to enter the data all on one line and user getInt etc... which may be what you're currently doing.

    There is no golden bullet for this problem because there's no way of returning multiple separate data from a Java method, and no pass-by-address, so you can't provide a format string (like C's scanf) and expect a method to assign variable of various types based on the content of a string. Scanner's get<Type>() methods are the closest you'll get.

Similar Threads

  1. Simple question
    By white97 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 11th, 2011, 06:30 PM
  2. Help with a simple question
    By allea in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 28th, 2011, 07:46 AM
  3. Just a simple question
    By newbie in forum Java Theory & Questions
    Replies: 10
    Last Post: December 6th, 2010, 08:19 PM
  4. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM