Blue Pelican Java Project 11?

Write a program that will allow a user to input his name. The prompt and input >Update:

here's what i got so far:

import java.io.*;

import java.util.*;

public class NameReversal{

public static void main(String [] args){

Scanner kbReader=new Scanner(System.in);

System.out.print("Please enter your name._");

String s=kbReader.nextLine();

int len=s.length();

...i just have no clue what to do with the for loop....

Comments

  • You can also solve the problem this way.

    import java.util.Scanner;

    public class NameReversal {

    public static void main(String[] args) {

    Scanner kbReader = new Scanner(System.in);

    System.out.print("Please enter your name._");

    String s = kbReader.nextLine();

    int len = s.length();

    String reversedString = "";

    for(int i = 0; i < len; i++) {

    int j = len - i - 1;

    reversedString += s.substring(j, j+1).toLowerCase();

    }

    System.out.println(reversedString);

    }

    }

    I prefer this solution because the for loop clear goes from 0 to the length of the input.

    I use a trick to calculate out the reverse string by making the index go backwards. It's possible to combine this logic into the for loop, but that's more confusing for me.

  • Yeah, do you have some sample code? You should at least try, it's nothing too hard, and i'll help out.

    Try implementing this:

    First, add a String reverseName="";

    for(int i=s.length()-1; i>=0; i--) {

    reverseName+=s.charAt(i);

    }

    EDITED: look at the for loop again.

    After the loop (outside of it), add:

    reverseName = reverseName.toLowerCase();

    And print reverseName to output. That should be all.

    Let me know if you have problems.

  • For the best answers, search on this site https://shorturl.im/awL2G

    since the main class is tester,you cant able to create object for circle and use it, Try this code: class Circle { Circle(double d) { System.out.println(d*2); } } public class Tester extends Circle { Tester(double d) { super(d); } public static void main( String args[]) { Circle cirl = new Circle (35.5); } } or class Circle { void diameter(double d) { System.out.println(d*2); } } public class Tester extends Circle { public static void main( String args[]) { Circle cirl = new Circle (); cirl.diameter(35.5); } }

  • One curly brace should be right and the other outside left. You have them both facing the same direction, for both sets.

Sign In or Register to comment.