Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: 1 MINOR ERROR IN PROGRAM

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 1 MINOR ERROR IN PROGRAM

    Hi,
    here is my program:

    package practice;
     
    import java.util.*;
    public class New2 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String [] words= {"write", "that", "program", "fusion"};
     
    		Scanner in= new Scanner(System.in);
    		char newGame;
     
    		{
    		int index= (int)(Math.random() * words.length);
     
    		String word= words[index];
    		StringBuilder guess= new StringBuilder();
     
    		for (int i=0; i < word.length(); i++){
    			guess.append('*');
    		}
    		int CorrectGuesses=0;
    		int Misses=0;
     
    		while (CorrectGuesses < word.length()){
    			System.out.println("(Guess) Enter a letter in word " +guess+ " > ");
    			String s= in.nextLine();
    			char letter= s.charAt(0);
     
    		if (guess.indexOf(letter) >= 0){
    			System.out.println("\t" +letter+ " is already in the word");}
    		else if (word.indexOf(letter) < 0) {
    			System.out.println("\t" +letter+ " is not in the word");
    			Misses++;}
    		else {
    			int h= word.indexOf(letter);
    			while (h >= 0) {
    				guess.setCharAt(h, letter);
    				CorrectGuesses++;
    				h= word.indexOf(letter, h+1);
     
    				}		
    			}
    		}
    		System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s).");
     
    		System.out.println("Do you want to guess another word? Enter y or n>");	
    	}
     
    	}
    }

    I have one error: the line >> if (guess.indexOf(letter) >= 0){
    This line is preventing my program from running

    The error: The method indexOf(String) in the type StringBuilder is not applicable for the arguements (char).


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    because you are passing an argument/parameter of type char, as StringBuilder (Java Platform SE 7 ) says, indexOf method accepts a String object, not a char primitive type.
    you better parse/convert your letter of type char into String object.
    please:
    String (Java Platform SE 7 ) or
    Character (Java Platform SE 7 )
    to know how to convert char to String.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    I decided to practice it with files instead since it is one of y weak areas so here is the updated program:

    package practice;
     
    import java.util.*;
    import java.io.*;
     
    public class New2 {
    	public static void main(String[] args) throws IOException{
    		String [] words= new String [1000];
    		Scanner in= new Scanner(new FileReader("list.txt"));
    		[] words= in.nextLine();
    		//Scanner in= new Scanner(System.in);
    		char newGame;
     
    		{
    		int index= (int)(Math.random() * words.length);
     
    		String word= words[index];
    		StringBuilder guess= new StringBuilder();
     
    		for (int i=0; i < word.length(); i++){
    			guess.append('*');
    		}
    		int CorrectGuesses=0;
    		int Misses=0;
     
    		while (CorrectGuesses < word.length()){
    			System.out.println("(Guess) Enter a letter in word " +guess+ " > ");
    			String s= in.nextLine();
    			char letter= s.charAt(0);
     
    		if (guess.indexOf(letter) >= 0){
    			System.out.println("\t" +letter+ " is already in the word");}
    		else if (word.indexOf(letter) < 0) {
    			System.out.println("\t" +letter+ " is not in the word");
    			Misses++;}
    		else {
    			int h= word.indexOf(letter);
    			while (h >= 0) {
    				guess.setCharAt(h, letter);
    				CorrectGuesses++;
    				h= word.indexOf(letter, h+1);
     
    				}		
    			}
    		}
    		System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s).");
     
    		System.out.println("Do you want to guess another word? Enter y or n>");	
    	}
        close.in();
    	}
    }

    My 2 ERRORS:
    1. I have one error: the line >> if (guess.indexOf(letter) >= 0){
    This line is preventing my program from running

    The error: The method indexOf(String) in the type StringBuilder is not applicable for the arguements (char)


    2. I have one error: the line >>Scanner in= new Scanner(new FileReader("list.txt"));

    The error: syntax error on token ";" , PrimitiveType expected after this token

    --- Update ---

    Thanks for the pointer on the first error, where did i go wrong with the second error in my updated post above?
    Last edited by Java girl; April 13th, 2014 at 03:41 PM.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    2. I have one error: the line >>Scanner in= new Scanner(new FileReader("list.txt"));

    The error: syntax error on token ";" , PrimitiveType expected after this token
    actually you a got a syntax error there. look at this code of yours:
    String [] words= new String [1000];
    Scanner in= new Scanner(new FileReader("list.txt"));
    [] words= in.nextLine();

    look at the last line of your codes, that is invalid syntax, if you want to put a value in specified element of your array, brackets should be after the variable name and it must contain an index.
    example:

    words[0] = "Java Girl";
    that statement declares first element of String array words is "Java Girl".

    1. I have one error: the line >> if (guess.indexOf(letter) >= 0){
    This line is preventing my program from running

    The error: The method indexOf(String) in the type StringBuilder is not applicable for the arguements (char)
    Please read my above reply, (#2)

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    My fixing the syntax error, the other error removed and I was able to run my program:

    package practice;
     
    import java.util.*;
    import java.io.*;
     
    public class New2 {
    	public static void main(String[] args) throws IOException{
    		String words []= new String [1000];
    		Scanner in= new Scanner(new FileReader("list.txt"));	
    		words [1000]= in.nextLine();
     
     
    		{
    		int index= (int)(Math.random() * words.length);
     
    		String word= words[index];
    		StringBuilder guess= new StringBuilder();
     
    		for (int i=0; i < word.length(); i++){
    			guess.append('*');
    		}
    		int CorrectGuesses=0;
    		int Misses=0;
     
    		while (CorrectGuesses < word.length()){
    			System.out.println("(Guess) Enter a letter in word " +guess+ " > ");
    			String s= in.nextLine();
    			char letter= s.charAt(0);
     
    		if (guess.indexOf(letter) >= 0){
    			//System.out.println("\t" +letter+ " is already in the word");}
    		if (word.indexOf(letter) < 0) {
    			System.out.println("\t" +letter+ " is not in the word");
    			Misses++;}
    		else {
    			int h= word.indexOf(letter);
    			while (h >= 0) {
    				guess.setCharAt(h, letter);
    				CorrectGuesses++;
    				h= word.indexOf(letter, h+1);
     
    				}		
    			}
    		}
    		System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s).");
     
    		System.out.println("Do you want to guess another word? Enter y or n>");	
    	}
        in.close();
    	}
    }


    I got the error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000
    at practice.New2.main(New2.java:12)
    Last edited by Java girl; March 30th, 2014 at 10:12 PM.

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    look at this part of your code
    String words [1000]= new String " ";
    that is an invalid declaration of array as what Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) says,
    it should be like this:
    String[] myArray = new String[desired length] or
    String myArray[] = new String[desired length]
    NOT
    String words [1000]= new String " "; that is an invalid syntax for declaration of array.

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    Same run time error exists even after the correct format but thanks

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    oh,, ArrayIndexOutOfBoundsException means that you attempted to access an element whose index is beyond the size.
    Index in array always starts with 0, and usually ends with the length - 1.

    so if the length of your array is 10, accessing element 10 would throw ArrayIndexOutOfBounds Exception. because the last element lies at index 9.
    Element Array Index
    First array[0]
    Second array[1]
    Third array[3]
    .
    .
    .
    Tenth array[9]

  9. #9
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    This is how the file i am attempting to read from is layout:
    Umbrella
    Rain
    Jacket
    Boots
    Geography
    Pizza
    Music
    Disco
    Roti
    Optician
    Violin


    Any string is suppose to be selected from it

  10. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    did you read it successfully? what is the problem now?

  11. #11
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    Well, I am still getting the ArrayIndexOutOfBoundsException error so I am unable to run it successfully.
    Here is my current program:

    package practice;
     
    import java.util.*;
    import java.io.*;
     
    public class New2 {
    	public static void main(String[] args) throws IOException{
    		//String [] words= new String [1000];
    		String words []= new String [20];
    		Scanner in= new Scanner(new FileReader("list.txt"));
    		words [] = in.nextLine();
     
    		{
    		int index= (int)(Math.random() * words.length);
     
    		String word= words[index];
    		StringBuilder guess= new StringBuilder();
     
    		for (int i=0; i < word.length(); i++){
    			guess.append('*');
    		}
    		int CorrectGuesses=0;
    		int Misses=0;
     
    		while (CorrectGuesses < word.length()){
    			System.out.println("(Guess) Enter a letter in word " +guess+ " > ");
    			String s= in.nextLine();
    			char letter= s.charAt(0);
     
    		//if (guess.indexOf(letter) >= 0){
    			//System.out.println("\t" +letter+ " is already in the word");}
    		if (word.indexOf(letter) < 0) {
    			System.out.println("\t" +letter+ " is not in the word");
    			Misses++;}
    		else {
    			int h= word.indexOf(letter);
    			while (h >= 0) {
    				guess.setCharAt(h, letter);
    				CorrectGuesses++;
    				h= word.indexOf(letter, h+1);
     
    				}		
    			}
    		}
    		System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s).");
     
    		System.out.println("Do you want to guess another word? Enter y or n>");	
    	}
        in.close();
    	}
    }

  12. #12
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    at what part/line do get it? I'm sure it is saying a line number on which you got an error.
    is that really the error? because this line words [] = in.nextLine(); will make you have compilation error?

  13. #13
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    yes,words [] = in.nextLine(); is causing the compilation error

  14. #14
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    because that is invalid syntax, that statement is attempting to put a value in specified index of array, but you did not provide any index on it.
    words[0] = in.nextLine(); 0 inside the bracket is the index of array where I want to put the return value of in.nextLine();
    in short, you should an integer inside that bracket that is greater than and not equal to the size of your array.

  15. #15
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    or, well I fixed that but the error is now:
    Exception in thread "main" java.lang.NullPointerException
    at practice.New2.main(New2.java:19)

  16. #16
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    Can you paste your updated code here?
    can you paste also the line 19 of your code? thanks.

  17. #17
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    package practice;
     
    import java.util.*;
    import java.io.*;
     
    public class New2 {
    	public static void main(String[] args) throws IOException{
     
    		String words []= new String [20];
    		Scanner in= new Scanner(new FileReader("list.txt"));
    		words [0] = in.nextLine();
     
    		{
    		int index= (int)(Math.random() * words.length);
     
    		String word= words[index];
    		StringBuilder guess= new StringBuilder();
     
    		for (int i=0; i < word.length(); i++){
    			guess.append('*');
    		}
    		int CorrectGuesses=0;
    		int Misses=0;
     
    		while (CorrectGuesses < word.length()){
    			System.out.println("(Guess) Enter a letter in word " +guess+ " > ");
    			String s= in.nextLine();
    			char letter= s.charAt(0);
     
    		//if (guess.indexOf(letter) >= 0){
    			//System.out.println("\t" +letter+ " is already in the word");}
    		if (word.indexOf(letter) < 0) {
    			System.out.println("\t" +letter+ " is not in the word");
    			Misses++;}
    		else {
    			int h= word.indexOf(letter);
    			while (h >= 0) {
    				guess.setCharAt(h, letter);
    				CorrectGuesses++;
    				h= word.indexOf(letter, h+1);
     
    				}		
    			}
    		}
    		System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s).");
     
    		System.out.println("Do you want to guess another word? Enter y or n>");	
    	}
        in.close();
    	}
    }

    line 19: for (int i=0; i < word.length(); i++){

  18. #18
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    Oh I get it now. Here is what happened at back of your code:
    you declared a String array whose length is 20,
    so by default, all elements of your array is null (a null pointer).
    next you wrote something in first element of your array, words[0] = in.nextLine(); so first element of your String array has a String value, and the rest is null.
    next you generated a random integer and make it as the index.
    and you declared word = words[index];
    if the value of index is not 0 (since it is randomly generated), you put a null pointer in String word.
    Something like this String word = null;
    did you get it? because all (except for first element) element of your array is null.

    I think you better put a String value on each element of your array first.

  19. #19
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    ok so how do i go about putting a String value on each element of the array ?

  20. #20
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: 1 MINOR ERROR IN PROGRAM

    I think you assumed that you have 20 words in your file (separated by carriage return/enter).
    so read each line and store it in every element of your array.
    words[0] = first word of file.
    words[1] = second word of file
    words[19] = last word of file

Similar Threads

  1. Hangman minor issues
    By Tigers27 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 6th, 2013, 08:13 PM
  2. Beginner Help Runs but has Minor Error!
    By laxinnn in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 13th, 2013, 11:06 AM
  3. Need some minor help.
    By XxDarkstarxX in forum What's Wrong With My Code?
    Replies: 58
    Last Post: February 10th, 2013, 09:56 PM
  4. Java vending machine, minor thing.
    By Aprok in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2011, 02:35 PM
  5. [SOLVED] Please help with this... I think I have a minor coding error
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:02 AM