-5

(preface) I actually believe this to be a simple question but im fairly new to programming so it is a bit frustrating for me. This questions pertains to a problem with how uppercase and lowercase string may override each other when displaying executed code.

Code below:

import java.util.Scanner; //need for the scanner class

public class Manip
{   
    public static void main(String[] args)
    {
        String city; //to hold the user input   

        //create the scanner object here
        Scanner keyboard = new Scanner(System.in);

        //get the favorite city
        //prompt the user to input the favorite city
        System.out.print("Enter favorite city: ");      

        //read and store into city
        city = keyboard.nextLine();

        //display the number of characters.
        System.out.print("String Length :" );
        System.out.println(city.length());

        //display the city in all upper case
        System.out.println(city.toUpperCase() );

        //Display the city in all lower case
        System.out.println(city.toLowerCase() );

        //Display the first character

        keyboard.next();
    }
}

the output of the code is-

enter favorite city: dallas
String Length :6
dallas

the desired output is-

enter favorite city: dallas
String Length :6
DALLAS
dallas

I'm wondering why it is not printing "dallas" twice, as uppercase AND lowercase letters. it seems to be overwriting the input

ben
  • 11
  • 4
  • 2
    Please post all pertinent code here as code-formatted text, and not as an image. None of us can copy your image into our IDE's and then compile and run it or modify it. An image of code does us little good. Best in fact if you could post a valid [sscce](http://sscce.org), a very small program that compiles and runs for us and that shows us your problem. – Hovercraft Full Of Eels Apr 28 '17 at 03:21
  • 3
    And same for the display. Most important, please go through the [help] sections on How-to-Ask, and take a look at [I'm new to SO, what are some things I should do, and what things will I likely want to do, that I shouldn't?](https://meta.stackoverflow.com/q/347937/522444) – Hovercraft Full Of Eels Apr 28 '17 at 03:22
  • You aren't running the code you think you are (you probably need to recompile). And, **never** post code as images. `System.out.printf("%s (length = %d) %s %s%n", city, city.length(), city.toLowerCase(), city.toUpperCase());` – Elliott Frisch Apr 28 '17 at 03:23
  • works fine for me buddy. – Benji Weiss Apr 28 '17 at 03:55
  • @Ben I don't see any error here, but I do have a question. Why is the following line at the end of your code `keyboard.next();`? is there any reason for this line? –  Apr 28 '17 at 03:56
  • ok so the problem is that I need the code to output dallas in lowcase, and in uppercase letters. so DALLAS dallas but its only outputting in lowercase letters. I think the "toUppercase" and "toLowercase" commands are overwriting each other. ("is there any reason for that line") oh I suppose I left that part of the code on towards the end. it doesnt do anything its part of a longer code for an assignment. – ben Apr 28 '17 at 04:06

1 Answers1

0

I ran your code just as it is, and it printed it both in uppercase and lowercase, but I noticed you had a comment that said //Display the first character, and following it, keyboard.next(). This was the only incorrect part. To print the first letter of a string(in this case the string is city), use this line: System.out.println(city.substring(0,1).toUpperCase()); below is an example of the output:

Enter favorite city: dallas.          
String Length :6
DALLAS
dallas
D
  • there's not much to answer here, but I thought i would say something... –  Apr 28 '17 at 04:09
  • @ben be sure to close your `Scanner` method, using `keyboard.close()` after there is no more input needed. This is not necessary, but advised for neatness –  Apr 28 '17 at 04:11
  • Hm this made it work. I didnt notice I had that towards the end, perhaps it was overwriting the output. Thank you for addressing that question as well . it was what I needed to figure out next =/, thank you for your help. – ben Apr 28 '17 at 04:27
  • @ben You can accept an answer if it helped you. – Sam Apr 28 '17 at 04:34