samedi 21 février 2015

Having issues with basic Java code

I've included below my entire assignment for my computer science class. I understand that this is a very basic Java code to write but the part that is specifically throwing me off is the part that asks to print the same sting that is typed in, but doubling the letters... I'm not sure how to write this into my code. So I named my sting "A" yet I'm not sure how to get it to double. Here is my progress so far:



import java.util.Scanner;
public class DoubleLetters{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
String A=scan.nextLine();
}
}



Write a program in a single class called DoubleLetters that does the following: The program should read in a single line of text from the keyboard. It should then print to the console the same line of text, only now with every letter doubled. Other characters, such as numbers, spaces, and punctuation in the line should be left alone, except for exclamation points (!): These should be tripled.


The objectives of this assignment are to:


Be able to read text input from the keyboard using a Scanner object. Be able to traverse the characters in a String using String class methods and a for loop. Be able to use an if statement in a loop.


Example 1: if the input text is:


Hey! My car can't move!!


Then your program should print:


HHeeyy!!! MMyy ccaarr ccaann'tt mmoovvee!!!!!!


Example 2: if the input text is:


Hello, I have 9 of them!


Then your program should print:


HHeelllloo, II hhaavvee 9 ooff tthheemm!!!


Hint:

When is a character a letter? There is more than one way to tell. Look up the Character class in the Java API. There is a method that would be very helpful.



thank you for any help that you can give me, I'm attempting to learn this on my own and any kind of help/tips is graciously accepted.


Aucun commentaire:

Enregistrer un commentaire