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: reading a string from input

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default reading a string from input

    Hi so I am wondering how it would be possible to read a string from input. for instance:
    public class TestInput1{
     
        public static void main (String[]args){
        double firstNo = readDouble("First number?");
        int operator = readInt("Operator - type 1 for *, 2 for +, 3 for -, or 4 for /");
        Double secondNo = readDouble("Second number?");
        if (operator == 1) {
            System.out.println(firstNo + " times " + secondNo + " = " + (firstNo*secondNo));
        }
        else if (operator == 2){
            System.out.println(firstNo + " plus " + secondNo + " = " + (firstNo+secondNo));
        }
        else if (operator == 3){
            System.out.println(firstNo + " minus " + secondNo + " = " + (firstNo-secondNo));
        }
        else if (operator == 4){
            System.out.println(firstNo + " divided by " + secondNo + " = " + (firstNo/secondNo));
        }
        }
     
        public static int readInt(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);
    return keyboard.nextInt();
    }
    public static String readString(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);
    return keyboard.nextLine();
    }
    public static double readDouble(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);
    return keyboard.nextDouble();
    }
    }

    When I first wrote this, I had it so that the user typed in the actual operator e.g. * or + however when I went to check the variable like this:

    public class TestInput1{
     
        public static void main (String[]args){
        double firstNo = readDouble("First number?");
        String operator = readString("Operator --> *, +, -, or /");
        Double secondNo = readDouble("Second number?");
        if (operator == *) {
            System.out.println(firstNo + " times " + secondNo + " = " + (firstNo*secondNo));
        }
        else if (operator == +){
            System.out.println(firstNo + " plus " + secondNo + " = " + (firstNo+secondNo));
        }
        else if (operator == -){
            System.out.println(firstNo + " minus " + secondNo + " = " + (firstNo-secondNo));
        }
        else if (operator == /){
            System.out.println(firstNo + " divided by " + secondNo + " = " + (firstNo/secondNo));
        }
        }
     
        public static int readInt(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);
    return keyboard.nextInt();
    }
    public static String readString(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);
    return keyboard.nextLine();
    }
    public static double readDouble(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);
    return keyboard.nextDouble();
    }
    }

    it didn't like it, and wouldn't check the variable, an error just appeared. I didn't post this in "what's wrong with my code?" because I solved the problem, but I would like to know how to solve this in future without the crude int variable instead.
    thanks,
    Skeptile


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading a string from input

    it didn't like it,
    Please explain and post the full text of any error messages
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: reading a string from input

    It would help if you compared strings using .equals() instead of ==

Similar Threads

  1. Replies: 11
    Last Post: November 12th, 2012, 01:09 PM
  2. Reading input into an ArrayList
    By thesoulpatchofBruce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2012, 02:00 PM
  3. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM
  4. Reading String Input?
    By Morevan in forum Java Theory & Questions
    Replies: 1
    Last Post: January 18th, 2010, 12:16 PM
  5. reading JMF input streams?
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 12th, 2010, 12:22 AM