Java Assignment First, launch NetBeans and close any previous projects that may
ID: 3872089 • Letter: J
Question
Java
Assignment
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "BackwardsStrings" (without the quotation marks) that:
Prompts the user at the command line for one 3-character string.
Then (after the user inputs that first 3-character string) prompts the user for another 3-character string.
Then prints out the two input strings with a space between them.
Finally prints on a separate line the two input strings 'in reverse' (see example below) with a space between them.
So, for example, if the first string is 'usr' and the second string is 'bin', your program would output something like the following:
Note that the reversed SECOND string comes FIRST when printing the strings in reverse.
my result
/*
Name: Saima Sultana
Lab:PA - BackwardsStrings (Group 2)
*/
package backwardsstrings;
import java.util.Scanner;
public class BackwardsStrings {
public static void main(String[] args) {
//variables
String first, second;
// TODO code application logic here
StringBuilder firstReversed= new StringBuilder();
StringBuilder secondReversed= new StringBuilder();
Scanner input = new Scanner(System.in);
// read strings
first=input.nextLine();
second=input.nextLine();
// print out the input strings
System.out.println("The two string you entered are:"+ first+" "+second+".");
// reverse the strings
for ( int i=first.length()-1;i>=0; i--)
firstReversed.append(first.charAt(i));
for ( int i=second.length()-1;i>=0; i--)
secondReversed.append(second.charAt(i));
// print out the reverse string
System.out.println("The two strings in reverse are:"+secondReversed+" "+ firstReversed+ ".");
}
}
usr bin
usr bin
The two string you entered are:usr bin usr bin.
The two strings in reverse are:nib rsu nib rsu.
BUILD SUCCESSFUL (total time: 20 seconds)
I am not getting the same output as an the example shown actually i had to type twice to get the result.
please help
Explanation / Answer
Hi
I have modified the code and highlighted the code changes below
BackwardsStrings.java
import java.util.Scanner;
public class BackwardsStrings {
public static void main(String[] args) {
//variables
String first, second;
// TODO code application logic here
StringBuilder firstReversed= new StringBuilder();
StringBuilder secondReversed= new StringBuilder();
Scanner input = new Scanner(System.in);
// read strings
first=input.next();
second=input.next();
// print out the input strings
System.out.println("The two string you entered are:"+ first+" "+second+".");
// reverse the strings
for ( int i=first.length()-1;i>=0; i--)
firstReversed.append(first.charAt(i));
for ( int i=second.length()-1;i>=0; i--)
secondReversed.append(second.charAt(i));
// print out the reverse string
System.out.println("The two strings in reverse are:"+secondReversed+" "+ firstReversed+ ".");
}
}
Output:
usr bin
The two string you entered are:usr bin.
The two strings in reverse are:nib rsu.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.