-1

I have tried many different things and nothing has worked. I am receiving an error "The local Variable line may not have been initialized Java(536870963) [Ln 44, Col15]" How do I fix this? I'm not finding anything that has help me understand the error of my ways. Please help.

import java.util.Scanner;
import java.util.Arrays;

public class A8johnston<close> {
public static String [][] data = new String[33][5];
public static void main(String[] args){

 // Calling methods to use   
    initializeTable();
    loadHistoricalData();
    currencyLookup();
   
    for (int i = 0; i < data.length; i++) 
       
    System.out.println(Arrays.toString(data[i]));
    }
   // Definde method intializeTable
    private static void initializeTable(){    
    for (int i=1; i<data.length;i++){
            data[i][0]= Integer.toString(i);
            for (int j=1;j<data[0].length; j++){
                data[i][j]="Unavailable";
            }
        }
        
    }
// Definde method currencyLookup
 
   
    private static void currencyLookup() {
 
    String currency;
    String[] row=null;
    int day;
    boolean isCurrencyFound = false;
    double amount,convertedAmount;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter Currency Loopup data (Currency, Date of Month, Amount): ");

    while (true){
           convertedAmount=0;
    isCurrencyFound=false;
    String line;
    row = line.split(",",-1);
    currency=row[0];
    day=Integer.parseInt (row[2]);
    amount=Double.parseDouble(row[2]);
    if(currency==null || currency.trim()=="" || currency.trim().length()==0){
        break;
    }
    //Calculate the Amount
    int i;
    for (i=1;i<data[0].length;i++){
        if(data[0][i].equalsIgnoreCase(currency)){
            isCurrencyFound=true;

            if(("Unavailable").equalsIgnoreCase(data[day][i])){
                System.out.println("Data not available");
                break;
            }
            convertedAmount=amount * Double.parseDouble(data[day][i]);
            break;
        }
    }
     // What to do if curency is found
    if(isCurrencyFound)
    System.out.println("Allowed Currency;"+Arrays.toString(data[0]));
    System.out.println("Enter Currency Loopup Data (Currency, Date of Month, Amount): ");
    line=scanner.next();
    }
}  
// Gather historical Data and store it in the array 
private static void loadHistoricalData() {
        String line = null,rate,currency;
        boolean isCurrencyAllowed=false;
        int day;
        String[] row;
        Scanner scanner =new Scanner(System.in);
        System.out.println("Enter Historical Data (Currency, Date of Month, Rate): ");
        line =scanner.next();
        
        while(true){
            isCurrencyAllowed=false;
            row =line.split(",", -1);
            currency=row[0];
            day=Integer.parseInt(row[1]);
            rate=row[2];
            if(currency ==null || currency.trim() =="" || currency.trim().length()==0){
                break;
            }
            for(int i=1;i<data[0].length;i++){
                if(data[0][i] =="" || data[0][i] ==null){
                    data[0][i] =currency;
                    data[day][i]=rate;
                    isCurrencyAllowed=true;
                    break;
                }
                if(currency.equalsIgnoreCase(data[0][i])){
                    data[day][i] =rate;
                    isCurrencyAllowed=true;
                    break;
                }
            }
            if (isCurrencyAllowed) System.out.println("Only 3 Currency         Allowed:"+Arrays.toString(data[0]));
            System.out.println("Enter Historial Data (Currency, Date of Month, Rate): ");
            line =scanner.next();
        }
        System.out.println("Data Loading Done");
    }
        close scanner;
    }

Its not working for me. Please help.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

2 Answers2

2

Line 44 reads row = line.split(",", -1). This is a problem because line 43 reads String line;. This is an uninitialized variable, which Java won't let you use. Try writing String line = scanner.nextLine() instead.

While I'm at it, line 69 (line = scanner.next()) should be deleted, as leaving it in would result in two consecutive scanner reads, which means you're skipping over input that could be important.

Furthermore, the while (true) condition should probably be replaced with while (scanner.hasNext() ). This eliminates the possibility of a NoSuchElementException being thrown if the scanner tries to read something that doesn't actually exist.

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
0

The error is caused by the fact that you are trying to use the line variable without initializing it first. To fix this error, you need to initialize the line variable before using it in the currencyLookup() method.

Ariel G
  • 66
  • 5