-4

I am working on a program that allows the user to input an integer, double, character, and a string. I have used variables to store the numbers in I am using BlueJ as my IDE, and my question is how I can reverse a system.out.println line that has variables in it?

Here is my code below:

import java.util.Scanner;
import java.lang.String;
public class Lab1
{  
    public static void main(String[] args)
    {
         Scanner input = new Scanner(System.in);
        //Entering a integer

        int money = 0;
        System.out.println("Enter an integer:");
        money = input.nextInt();

        //entering a double
        double cost = 10;

        System.out.println("Enter a double:");
        cost = input.nextDouble();

        //Entering a character
        char a;
        System.out.println("Enter a character:");
        a = input.next().charAt(0);

        //Entering a string

        System.out.println("Please enter a string:");
        String string = input.next();
        System.out.println();

        //Single line separated by spaces
        int num = money;
        double price = cost;
        char b = a;
        String text = string;
        System.out.println("Single line:");
        System.out.print(num + " " + price + " " + b + " " + text);

        //Values in reverse

        System.out.println();
        System.out.println();
        System.out.println("Values in reverse:"); 
    }
}

Note: I am using BlueJ for this, and I have tried reversing the variables but, I couldn't.

My output:

Enter an integer: 45 Enter a double: 98.32 Enter a character: a Please enter a string: welcome

Single line: 45 98.32 a welcome

The reverse of '45 98.32 a welcome' should be:

Welcome a 98.32 and 45.

Thank you and have a great day.

Shivam Kumar
  • 1,839
  • 1
  • 20
  • 30
NightCode
  • 29
  • 5

1 Answers1

1
System.out.println("Values in reverse:");
System.out.print(text + " " + b + " " + price + " " + num)

For this simple question, I thought you need to change the order of output then it would be fine? Anyway, if this is not your expected output, do tell me so.

In case this is what you are looking for, reverse() method is for the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder which meant you can only reverse the string output rather than the println in Java.

RogerSK
  • 345
  • 1
  • 16
  • I know that I can switch the values to their opposites but, I want to know if there is a code that I can add like a .reverse code or something like that. so that I can show that there is a code that changes it and that I didn't change the actual variables – NightCode Sep 03 '18 at 02:25
  • 1
    In fact, reverse() method is for the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder which meant you can only reverse the string output rather than the `println` in Java. – RogerSK Sep 03 '18 at 02:33
  • thanks for your help this is the code that I what I was looking for. And again thanks for your help. :) – NightCode Sep 03 '18 at 02:58
  • You're welcome. Anyway if you don't mind, please kindly accept it as an answer, appreciate that. – RogerSK Sep 03 '18 at 03:21