0

I keep getting this error when reading from a text file it gets the first few numbers but then does this there also shouldnt be 0's in there, tried a lot of things not sure what to do. I have to be able to read the file numbers and then multiply specific ones could use some help thanks. (I was using the final println to check what numbers it was getting). Sorry forgot the error is this output 4 32 0 38 0 38 0 16 0 Error: For input string: ""

Here is my file:

Unit One
4
32 8
38 6
38 6
16 7

Unit Two
0

Unit Three
2
36 7
36 7

Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5

Unit Five
4
32 6.5
32 8
32 7
32 8

Unit Six
5
38 7
30 6.5
24 8
24 8
24 8

Unit Seven
0

Unit Eight
1
40 12

Unit Nine
5
24 8
24 6.5
30 6.5
24 7
32 7

And here is my code:

package question;

import java.io.*;
import java.util.Scanner;


public class Weight
{
    

    
        public static void main(String[] args)//main method
        {
            try
            {
                
                Scanner input = new Scanner(System.in);
                //System.out.print("Please enter proposed weight of stock : ");
                // int proposedWeight = input.nextInt();
                
                File file = new File("MathUnits.txt");
                System.out.println(file.getCanonicalPath());
                FileInputStream ft = new FileInputStream(file);

                DataInputStream in = new DataInputStream(ft);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strline;

                while((strline = br.readLine()) != null)
                {   int i = 0;
                    if (strline.contains("Unit"))
                    {
                        continue;
                    }
                    else {
                String[] numstrs = strline.split("\\s+"); // split by white space
                int[] nums = new int[numstrs.length];
                nums[i] = Integer.parseInt(numstrs[i]);
                
                     for(int f = 0; f <numstrs.length; f++)
                     {
                    System.out.println(""+ nums[f]);
                     }
                    }
                    i ++;
                }
                //int x = Integer.parseInt(numstrs[0]);
               // int m = Integer.parseInt(numstrs[1]);
               // int b = Integer.parseInt(numstrs[2]);
               // int a = Integer.parseInt(numstrs[3]);
                  int a = 0;
                //   System.out.println("this >>" + x + "," + m +"," + b + "," + a);
                   
                //   if(proposedWeight < a)
                 //   {
                //       System.out.println("Stock is lighter than proposed and can hold easily"); 
                 //       System.exit(0);

                       
                //    }
                //    else if ( a == proposedWeight)
                //    { 
                        
                    //  System.out.println("Stock is equal to max");   
                    //  System.exit(0);
                  //  }
                  // else
                  // {
                    //   System.out.println("stock is too heavy");
                    //   System.exit(0);

                      
                 //  }
                   
                
                
        
                
                in.close();
            }
            catch(Exception e)
            {
                System.err.println("Error: " + e.getMessage());
            }
            
            
            }
            

        }
4
  • 3
    Not sure which error you are referring to Commented Nov 27, 2013 at 22:45
  • try doing strline.split(" "); Commented Nov 27, 2013 at 22:50
  • added the error now i forgot to add it in it is this > 4 32 0 38 0 38 0 16 0 Error: For input string: "" it shouldnt have the 0's and the input string error also Commented Nov 27, 2013 at 23:28
  • I tried strline.split("") didnt work :/ Commented Nov 27, 2013 at 23:30

1 Answer 1

1

One probable error I see

  • You're not taking newlines into consideration, especially when you're doing a nums[i] = Integer.parseInt(numstrs[i]); on an empty string

Also, I don't think you're getting the numbers into the array correct since you're getting only one int from each line, when some lines have two

Sign up to request clarification or add additional context in comments.

9 Comments

do u know what the best way to take the empty line into consideration would be? Whats the best way to fix the array?
Add an if statement to check for a new line and for the array, you have the right idea in the code, check your nums[i] = Integer.parseInt(numstrs[i]); and ask yourself, how many times will it run when you read one line? and how many times would you need it to run, look in the two lines above it (you'd prob just need a for loop)
ok i think I understand that part any idea why im getting the 0's in there?
it goes back to the not adding numbers into your array correctly, if you made an array of size 2, it defaults the values to zero. So if you only added one int from your line into the array, the other value in the array would still be zero because you never added the second value into it.
your output " 4 32 0 38 0 38 0 16 0" notice how the second value is never outputted correctly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.