-2
 import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int num=scan.nextInt();
    String array[]=new String[num];
    for(int i=0;i<array.length;i++){
      array[i]=scan.next();
    }
    int p=0,p1=0;
    String s="";
    String s1="";

    for(int i=0;i<array.length;i++){
      if(array[i].charAt(i)=='+'){
        s+=array[i].substring(i+1);
        s1+=array[i].substring(0,i);

      }
      int p=Integer.parseInt(s.trim());
      int p1=Integer.parseInt(s1.trim());
       System.out.println(p+p1);

    }  
  }
}

Iam getting an error saying number format exception but i tried everything to fix it but cant find a solution. Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException. this is the stack trace i got any help would be much appreciated thanks

brazil
  • 11
  • 4

3 Answers3

-1

try doing

    s1+=array[i].substring(0,i-1);

because at index 'i' you have '+'

Edit:

You are reading an integer input here:

    int num=scan.nextInt();

and then you are converting that integer into a string. Try reading a string directly like:

    Scanner scanner = new Scanner(System. in); 
    String input = scanner. nextLine();

and then parse the input

  • The `endIndex` is exclusive, meaning that `substring(0,i)` will not include `i`. – Joe C Mar 03 '18 at 21:56
  • still giving me the same error of number format exception@JoeC – brazil Mar 03 '18 at 22:09
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException. this is the stack trace i got – brazil Mar 03 '18 at 22:11
  • any way of fixing this ?@JoeC – brazil Mar 03 '18 at 22:13
  • the integer iam reading in is the array size im only converting the string array elements to an int – brazil Mar 03 '18 at 22:15
  • @brazil Don't put stack traces in a comment. You need to [edit] your question. – Joe C Mar 03 '18 at 22:25
  • @MariaNiaz Given that your answer is incorrect, I suggest that you delete it before it attracts more downvotes. – Joe C Mar 03 '18 at 22:25
  • there i edited it but i know this is wrong that is y im asking where its wrong @JoeC – brazil Mar 03 '18 at 22:37
-1

if i understand your question well, this should help adding two numbers like "2+2"
By splitting your string data "a+b" into two (a and b), then parsing each of the values a and b into integers making sure to to catch a NumberFormatException in case of a string instead of an integer etc..

for(int i=0;i<array.length;i++){
    try{
       String[] splitdata = array[i].split("\\+"); //splitting the data 
       s=splitdata[0];  // getting the first operand
       s1=splitdata[1]; //getting the second operand
       int p=Integer.parseInt(s); //parsing the first operand
       int p1=Integer.parseInt(s1); //parsing the second operand 
       System.out.println(p+p1); //adding
       }
       catch(NumberFormatException | ArrayIndexOutOfBoundsException e){
        // in case of wrong number format or array index exceeded
        System.out.println(e.getMessage());
       }
}

Test here: https://www.jdoodle.com/embed/v0/oQZ

tkc
  • 7
  • 4
  • While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Mar 06 '18 at 06:49
-1
import java.util.Scanner;

    class Main {
         public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int num=scan.nextInt();
        String array[]=new String[num];
        for(int i=0;i<array.length;i++){
          array[i]=scan.next();
        }

        String s="";
        String s1="";

        for(int j=0;j<array.length;j++){
            for(int i=0;i<array[j].length();i++){
                if(array[j].charAt(i)=='+'){
                   s +=array[j].substring(0, i);
                  s1 +=array[j].substring(i+1);

                }

          }



          int p=Integer.parseInt(s);
          int p1=Integer.parseInt(s1);
          System.out.println(p+p1);

        } 

      }
    }

Try this