0

When I compile the program below I get the message "variable x may not have been initialized" yet as far as I see I did. I am trying to have a program where a person selects a number from 1 to 10 and it then shows the corresponding string from the array.

import java.util.*;
import javax.swing.*;
public class JavaArray
{
  public static void main(String[] args)
  {
    String[] topJava = {"Challenging", "Fun", "Computer stuff", 
      "Learning new stuff", "Interesting", "Make my own programs", 
      "Solve my own problems", "Have jgrasp to help", "Lots of books to help",  "Tutorials online to help"};
    String[] choicesString = new String[10];
    String strSelectedJava;
    int selectedJava;
    int x;
    strSelectedJava = JOptionPane.showInputDialog(null, 
         "Choose a number between 1 and 10");
    selectedJava = Integer.parseInt(strSelectedJava);
    selectedJava = x;
    x = Arrays.binarySearch(topJava, selectedJava);
    if(x >= 0 && x < selectedJava)
        JOptionPane.showMessageDialog(null, "One reason I 
            like Java is:\n" + topJava);
    else
        JOptionPane.showMessageDialog(null,
             "Sorry - That number is not between 1 and 10");
    System.exit(0); 
    }
}

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by cgeier
0

You received this error because you used "x", but haven't yet assigned it a value.

int x;

//x is being used, but hasn't been assigned a value yet
selectedJava = x;

. Your code has other issues though. It throws a "ClassCastException" (thrown "if the search key in not comparable to the elements of the array").

See http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#binarySearch(int[],%20int)

for more info.

The code below should work.

import java.util.*;
import javax.swing.*;

public class JavaArray
{
 public static void main(String[] args)
 {
  String[] topJava = {"Challenging", "Fun", "Computer stuff",
"Learning new stuff", "Interesting", "Make my own programs",
"Solve my own problems", "Have jgrasp to help", "Lots of books to help", "Tutorials online to help"};
  int[] choices = {1,2,3,4,5,6,7,8,9,10};
  String strSelectedJava;
  int selectedJava;
  int x;
  
  strSelectedJava = JOptionPane.showInputDialog(null,"Choose a number between 1 and 10");
  selectedJava = Integer.parseInt(strSelectedJava);

  //sort the choices array to prepare for search
  Arrays.sort(choices);
  x = Arrays.binarySearch(choices, selectedJava);

  if(x >= 0 && x < selectedJava)
  {
    //don't forget to specify the array index: topJava[x]
    JOptionPane.showMessageDialog(null, "One reason I like Java is:\n" + topJava[x]);
  }
  else
  {
    JOptionPane.showMessageDialog(null,"Sorry - That number is not between 1 and 10");
  }

  System.exit(0);
 }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.