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

Thread: Simple question with scanning strings

  1. #1
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Simple question with scanning strings

    I'm new to java programming (about 1 week) and i've made up this simple program. The objective of the program is to make simple calculations but the problem is trying to input the operator. Here is the code:

    import java.util.Scanner;
     
    public class Main {
    	public static void main(String args[]) {
    		Scanner scanner = new Scanner(System.in);
     
    		int num1,num2,result;
    		String opr;
     
    		System.out.println("Input 1st number:");
    		num1 = scanner.nextInt();
     
    		System.out.println("Now the operator:");
    		opr = scanner.nextLine();
     
    		if(opr.equals("+")) {
    			System.out.println("Input 2nd number:");
    			num2 = scanner.nextInt();
     
    			result = num1 + num2;
    			System.out.println("Result: " + result);
    		}else if(opr.equals("-")) {
    			System.out.println("Input 2nd number:");
    			num2 = scanner.nextInt();
     
    			result = num1 - num2;
    			System.out.println("Result: " + result);
    		}else if(opr.equals("*")) {
    			System.out.println("Input 2nd number:");
    			num2 = scanner.nextInt();
     
    			result = num1 * num2;
    			System.out.println("Result: " + result);
    		}else if(opr.equals("/")) {
    			System.out.println("Input 2nd number:");
    			num2 = scanner.nextInt();
     
    			result = num1 / num2;
    			System.out.println("Result: " + result);
    		}else{
    			System.out.println("Please use a normal operator.");
    		}
    	}
    }

    What happens when i run the code is that it skips over scanning the opr variable and runs the else command. How can i fix this so i can input the operator?

    Just a simple begginer question and it would be awesome if you could help me out.
    Last edited by Crash; April 3rd, 2014 at 01:42 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Simple question with scanning strings

    problem is trying to input the operator
    Please explain what problem you are having.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,304
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: Simple question with scanning strings

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    And you say you have a simple question, but I don't see a question mark. What's the question?

  4. #4
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Simple question with scanning strings

    Sorry about that it's updated with the question now.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Simple question with scanning strings

    There is a problem using the Scanner class's nextLine() method after nextInt().
    See: Question About Scanner Class and nextInt(), nextLine(), and next() Methods
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Crash (April 3rd, 2014)

  7. #6
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Simple question with scanning strings

    Thanks a lot. Worked out and now it can run operations

Similar Threads

  1. Newbie question - print out strings from input
    By jcoltrin70 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 5th, 2013, 03:54 AM
  2. Simple computation program with strings...
    By alias22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 6th, 2012, 02:11 PM
  3. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 07:44 AM
  4. Question regarding Strings
    By yeeesh in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2010, 12:09 PM
  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