Results 1 to 3 of 3

Thread: Scanner Problem

  1. #1
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Scanner Problem

    I want to ask entr1 first then operation then entry 2. But not asking operation scanner. Jumping to loop while and asking entry2. Why?
    Java Code:
    import java.util.Scanner;
    
    public class Calculator {
    	public static void main(String[] args) {
    		
    		int entry1;
    		int entry2;
    		String operation;
    		boolean finish = true;
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("Sayı: ");
    		entry1 = scanner.nextInt();
    		System.out.print("Islem:");
    		operation = scanner.nextLine();
    		while (finish)  {
    			
    			System.out.println("Sayı2 :");
    			entry2 = scanner.nextInt();
    			switch (operation) {
    			case "+":
    				System.out.println(entry1 + entry2);
    				entry1 += entry2;
    				
    			case "-":
    				System.out.println(entry1 - entry2);
    				entry1 -= entry2;
    				
    			case "/":
    				System.out.println(entry1 / entry2);
    				entry1 /= entry2;
    				
    			case "*":
    				System.out.println(entry1 * entry2);
    				entry1 *= entry2;
    			case "=":
    				break;
    
    			default:
    				break;
    			}
    	
    			
    			System.out.println(entry1);
    			if (operation.equals("=")) {
    				finish = false;
    			
    			}
    			
    		}
    		System.out.println(entry1);
    		System.out.println("islem");
    		operation = scanner.nextLine();
    		
    		
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Scanner Problem

    Because after line 12 there is still a line delimiter in the buffer. So line 14 reads it and continues on. Clear the buffer
    with a nextLine right after line 12.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: Scanner Problem

    I solved like this.
    Java Code:
    import java.util.Scanner;
    
    public class Calculator {
    	public static void main(String[] args) {
    
    		float entry1;
    		int entry2;
    		String operation;
    		boolean finish = true;
    		Scanner s = new Scanner(System.in);
    		System.out.print("Sayı: ");
    		entry1 = s.nextInt();
    		Scanner s2 = new Scanner(System.in);
    		System.out.print("Islem:");
    		operation = s2.nextLine();
    
    		while (finish) {
    
    			System.out.print("Sayı :");
    			entry2 = s.nextInt();
    			switch (operation) {
    			case "+":
    				entry1 += entry2;
    				System.out.println(entry1);
    				break;
    			case "-":
    				entry1 -= entry2;
    				System.out.println(entry1);
    				break;
    			case "/":
    				entry1 /= entry2;
    				System.out.println(entry1);
    				break;
    			case "*":
    				entry1 *= entry2;
    				System.out.println(entry1);
    				break;
    			
    			default:
    				break;
    			}
    			System.out.print("Islem:");
    			operation = s2.nextLine();
    			if (operation.equals("=")) {
    				finish = false;
    			}
    			
    		}
    		System.out.println("İsleminiz bitti son değer :" + entry1);
    	}
    }

Similar Threads

  1. problem with scanner
    By fiction in forum New To Java
    Replies: 3
    Last Post: 02-03-2013, 12:32 AM
  2. Scanner and if/else problem.
    By edwed18 in forum New To Java
    Replies: 6
    Last Post: 11-12-2012, 05:47 PM
  3. max and min scanner problem
    By jwl in forum New To Java
    Replies: 2
    Last Post: 10-28-2012, 07:57 PM
  4. Scanner problem.
    By keo in forum New To Java
    Replies: 6
    Last Post: 04-07-2011, 08:30 AM
  5. Problem with scanner
    By JavaJ in forum New To Java
    Replies: 6
    Last Post: 03-16-2010, 08:51 PM

Posting Permissions

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