Results 1 to 1 of 1
  1. #1
    Tooks1 is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default need some help with my calculator(solved)

    Hi, i just started with my first program and i need some help.

    I build a calculator that is able to add, substract, multiply and divide two numbers.
    My aim is that while you can give any input it will only accept numbers when asked for numbers and +,*,/ or - when asked for a operator( and give you a message for the wrong input and let you try again). Now i get stuck on the operator part, because i try to scan a char in java.util*.

    This is the problem i think(as im kinda new) and i know you can get around it with try/catch, but i want to do it without because i do not fully understand it and i think it should be able to do a simple program without it and it wouldn't help me understand it. when i try to run the program it gives a StringIndexOutOfBoundsException: String index out of range:0 after you give the first number( so at the switch() point).

    So in conclusion how can i read a char within scan within the loop(because i want to put it in a loop so it can navigate back if a incorrect input is given) ?(Solved)

    Ok i already found how to read in the char. But then there is only one more question. At the end you have the option to make a new computation, but when you do it isn't posible to choose a operator(+,*,-,/) . How to fix this? I put the new code under the old one. (solved ) i had to interchange the y=1; on line 30 with line 29 in the second code.

    All answers are greatly appreciated

    Java Code:
     
    import java.util.*;
    
    public class adding {
    
    
    	
    
    	private static Scanner input;
    
    	public static void main(String[] args){
    	int a;	
    	int b;	
    	char o ;
    	
    		input = new Scanner(System.in);
    		
    		while(true){
    	      System.out.println("enter first number and press enterr:");
    	      if(input.hasNextInt()){
    	      a = input.nextInt();
    	      break;
    	      }
    	      else{
    	    	  System.out.println("THis isn't a number");
    	    	  input.next(); 
    	      }
    		}
    		switch(input.nextLine().charAt(0))
    		{
    		case'*':
    		o='*';
    		break;
    		case'+':
    	    o='+';
    	    break;
    		case'/':
    		o='/';
    		break;
    		case'-':
    		o='-';	
    		break;
    		default:
    			 System.out.println("this isn't a valid input");
    			 input.next();
    			return;
    		}
    		
    
    	      while(true){
    		 System.out.println("enter second number and press enter:");
    	      if(input.hasNextInt()){
    		  b = input.nextInt();
    		  break;
    	      }
    	      else{
    	    	  System.out.println("this isn't a number");
    	    	  input.next();  
    	      }
    	      }
    	      
    		  System.out.println("the answer is..:");
    		  
    		  if(o=='+')
    		  {	  
    		  int ans = a + b;
    		  System.out.println(a+"+"+b+"="+ans);
    		  }
    		  if(o=='*')
    		  {
    		  int ans = a * b;
    		  System.out.println(a+"*"+b+"="+ans);
    		  }
    		  if(o=='/')
    		  {
    		  int ans = a / b;
    		  System.out.println(a+"/"+b+"="+ans);
    		  }
    		  if(o=='-')
    		  {
    		  int ans = a-b;  
    		  System.out.println(a+"-"+b+"="+ans);
    		  }
    
    
    }
    
    
    }
    Here is the new code:
    Java Code:
    import java.util.*;
    
    public class adding {
    
    
    	
    
    	private static Scanner input;
    
    	public static void main(String[] args){
    	int a;	
    	int b;	
    	char o = '*';
    	int y = 1;
    	int z = 1;
    		input = new Scanner(System.in);
    	while(z>0){	
    		while(true){
    			
    	      System.out.println("Enter the first number:");
    	      if(input.hasNextInt()){
    	      a = input.nextInt();
    	      break;
    	      }
    	      else{
    	    	  System.out.println("It's not a numberl");
    	    	  input.next(); 
    	      }
    	     y = 1;
    		}
    		while(y>0)
    		{
    		System.out.println("enter a operator(*,+,/,-)");
    		switch(input.next().charAt(0))
    		{
    		case'*':
    		o='*';
    		y=0;
    		break;
    		case'+':
    	    o='+';
    	    y=0;
    	    break;
    		case'/':
    		o='/';
    		y=0;
    		break;
    		case'-':
    		o='-';
    		y=0;
    		break;
    		default:
    			 System.out.println("Invalid input");
    			 input.next();
    			 y=1;	 
    		}
    		
    		}
    	      while(true){
    		 System.out.println("Enter second number:");
    	      if(input.hasNextInt()){
    		  b = input.nextInt();
    		  break;
    	      }
    	      else{
    	    	  System.out.println("it's not a number");
    	    	  input.next();  
    	      }
    	      }
    	      
    		  System.out.println("the answer is:");	 
    		  
    		  if(o=='+')
    		  {	  
    		  int ans = a + b;
    		  System.out.println(a+"+"+b+"="+ans);
    		  }
    		  if(o=='*')
    		  {
    		  int ans = a * b;
    		  System.out.println(a+"*"+b+"="+ans);
    		  }
    		  if(o=='/')
    		  {
    		  int ans = a / b;
    		  System.out.println(a+"/"+b+"="+ans);
    		  }
    		  if(o=='-')
    		  {
    		  int ans = a-b;  
    		  System.out.println(a+"-"+b+"="+ans);
    		  }
    		  System.out.println("New computation Yes/No?");
    		  switch(input.next())
    		  {
    		  case"Nee":
    			  System.out.println("Bye!!");
    			  z=0;
    			  break;
    		  case"Ja":
    			  z=1;
    			  
    			  break;
    		default:
    			System.out.println("invalid input");
    			z=1;
    		break;
    		  }
    	}
    
    }
    
    
    }
    Last edited by Tooks1; 02-13-2013 at 09:04 PM.

Similar Threads

  1. Calculator
    By Elviii in forum New To Java
    Replies: 6
    Last Post: 01-06-2013, 02:43 AM
  2. calculator help
    By sirstroud in forum New To Java
    Replies: 0
    Last Post: 04-01-2012, 05:58 AM
  3. Need help with Calculator
    By Joshua4missions in forum New To Java
    Replies: 4
    Last Post: 12-10-2011, 02:44 AM
  4. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 05:49 PM
  5. Calculator
    By Moshe22 in forum New To Java
    Replies: 8
    Last Post: 01-17-2011, 05:29 AM

Posting Permissions

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