For this piece of code I have to change the arguments in the writeLine and writeBlock methods to char c, instead of String c. The problem is I dont know how to get the scanner to read in a character instead of a string. Is there any way to go about doing this and changing String c to char c in the writeLine and writeBlock methods?

public static void writeLine(String c, int n)
  {
    if(n==0){
     System.out.println();
    return;
  }
    else{
      System.out.print(c);
    }writeLine(c, n-1);
     
  }

  public static String writeBlock(String c, int m, int n)
  {
    if (m==0){
      System.out.println();
      return "";}
    else{
    writeLine(c, n);
    return (writeBlock(c, m-1, n));
  }
  }
  
  public static void main(String[] args)
  {
    int n;
    int m;
    String c;
    
    Scanner sc = new Scanner(System.in);
    
    System.out.println("input a character and two integers: ");
    
    //get the values
    c=sc.next();
    n=sc.nextInt();
    m=sc.nextInt();
    c.charAt(0);
    
    System.out.println(writeBlock(c, m, n));
   }
 
}

Recommended Answers

All 3 Replies

sorry I forgot to wrap the code

sorry I forgot to wrap the code

Then edit your post. You can edit it for up to 30 minutes later.

Scanner is only able to read string or the whole line that will return string again. So what you can do is to read scanner, get the string and use the string in one or other way

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.