1

import java.util.Scanner;

public class DisplayBox2 {

public static void box (int length, int width){ 

    Scanner input = new Scanner (System.in) ;
    String Answer; 

    System.out.println("Do you want to use a special character to use to display the box ?"); 
      Answer = input.nextLine();

    if (Answer == "Yes"){
        System.out.println("Please enter the character that you would like to display the box");
        int Char = input.nextInt(); 
    for (int i = 0; i < length; i++) {
           for (int j = 0; j < width; j++) {
                System.out.print(Char +" ");
            }
            System.out.println("");
        }
    }

    if (Answer == "No"){

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print(" *");
            }
            System.out.println("");
        }
    }


    input.close(); 
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner (System.in);  

    int length, width ; 

    System.out.println ("Please enter the length of the box"); 
    length = input.nextInt();
    System.out.println (" Please enter the width of the box");
    width = input.nextInt(); 

    input.close(); 


    box (length, width); 


}

}

i dont understand what the error is in my code. can anyone please help

Please enter the length of the box

5 Please enter the width of the box

5 Do you want to use a special character to use to display the box ?

Exception in thread "main" java.util.NoSuchElementException: No line found

at java.util.Scanner.nextLine(Unknown Source)

at methods.DisplayBox2.box(DisplayBox2.java:14)

at methods.DisplayBox2.main(DisplayBox2.java:56)

2 Answers2

1

I have corrected the code for you to work:

public static void box(int length, int width, Scanner input) {

        String answer;

        System.out.println("Do you want to use a special character to use to display the box ?");
        answer = input.next();
        if (answer.equals("Yes")) {
            System.out.println("Please enter the character that you would like to display the box");
            int Char = input.nextInt();
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    System.out.print(Char + " ");
                }
                System.out.println("");
            }
        }else if (answer.equals("No")) {
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    System.out.print(" *");
                }
                System.out.println("");
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            int length, width;
            System.out.println("Please enter the length of the box");
            length = input.nextInt();
            System.out.println(" Please enter the width of the box");
            width = input.nextInt();

            box(length, width, input);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally{
            input.close();
        }
    }

Points to note:

  • When you use input.close(), you are closing the System.in and not the Scanner and hence you are getting NoSuchElementException
  • Compare String only with equals() - compares values, other than '==' that compares references. It's a basic but common mistake for learners
  • Use try/catch/finally and especially finally to close/clean resources. If you use JDK 1.7 or higher, try with resources can help much better
Karthik R
  • 5,523
  • 2
  • 18
  • 30
  • Thanks man ! We didn't learn try/catch/finally yet although we do use JDK1.7 so i guess we learn about it in the future – Arbaaz Shakeel Sep 18 '15 at 04:52
  • @ArbaazShakeel Cool. Please close the thread by marking the answer if you think its resolved. Keep learning.. :) – Karthik R Sep 18 '15 at 04:57
  • @ArbaazShakeel What's that? any issues? – Karthik R Sep 18 '15 at 04:58
  • nvm that was a mistake – Arbaaz Shakeel Sep 18 '15 at 04:59
  • You can use try and finally alone. catch is optional. finally ensures that the code blocks in finally is executed even if there are exceptions. So, you safely close it at any case. However there are cases when finally won't execute. I will leave it to you to read. cheers. – Karthik R Sep 18 '15 at 05:00
-1

package methods;

import java.util.Scanner;

public class DisplayBox2 {

public static void box (int length, int width, String character){ 

    Scanner input = new Scanner (System.in) ;

    for (int i = 0; i < length; i++) {
           for (int j = 0; j < width; j++) {
                System.out.print(character +" ");
            }
            System.out.println("");
        }
    }





public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner (System.in);  

    int length, width ; 
    String Answer; 
    String character = ""; 

    System.out.println("Do you want to use a special character to use to display the box ?"); 
      Answer = input.next();

  if (Answer.equals("Yes")){
    System.out.println("Please enter the character that you would like to display the box");
     character =  input.next();
  }

  if (Answer == "No"){
      character = "*"; 
  }
    System.out.println ("Please enter the length of the box"); 
    length = input.nextInt();
    System.out.println (" Please enter the width of the box");
    width = input.nextInt(); 

    input.close(); 


    box (length, width, character); 


}
}

My answer.I didn't use try catch and finally but it works so its good.