954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String and characters

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));
   }
 
}
JConnor135
Newbie Poster
18 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

sorry I forgot to wrap the code

JConnor135
Newbie Poster
18 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
sorry I forgot to wrap the code


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

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

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 charAt(int index)
toCharArray()

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You