JAVA question a)Write a method called grabFirstToken that takes a single String
ID: 3847450 • Letter: J
Question
JAVA question
a)Write a method called grabFirstToken that takes a single String parameter and returns a String value. This method should return a String that is equivalent to a substring of the String parameter up to the first whitespace character (either space, tab, or newline). Your method should look for the first whitespace character in the parameter String, and return the substring up to (but not including) this first whitespace character. For example, if called with a parameter of “Three blind mice” your method should return: “Three”
b)Write a method called shuffleNames that takes an array of Strings as a parameter and returns a String value. This method should treat the Strings given by the parameter array as names, and re-combine them so that they will print out with each name in a different column instead of on a different line. The names should appear in the same order, left-to-right, as the order they are given in the parameter array. Your method should return a String that will contain each name in a different vertical column when printed, with spaces included to accommodate names of different lengths For example, if the names given in the parameter array were (in order) Athena, Apollo, Hera, Hermes, and Winston, then your method would return a String value that looked like this when printed: AAHHZW tpeeei horrun elamss nl e t ao s o n
c)Write a main method that prompts the user for six names and collects these in an array of Strings. If the user gives multiple names, or any String containing multiple words separated by whitespace, then you should only store the first word. That is, you should consider the user’s input, and only store their first word (up to the first occurrence of whitespace) as the inputted name. For example, if the user were to give “Harold Winston” after being prompted for a name, then you should only store “Harold”. Use the method from part (b) to print out the result of shuffling these names into vertical columns.
Explanation / Answer
import java.util.*;
public class TestProgram
{
public static String grabFirstToken(String s)
{ int ind=0;
String t;
ind=s.indexOf(" ");
if(ind ==-1)
ind=s.indexOf(" ");
if(ind>0)
t= s.substring(0, ind);
else t=s;
return t;
}
public static String shuffleNames (String n[])
{
int maxLen=0;
for(int i = 0; i < n.length; i++)
{
if(n[i].length()> maxLen)
maxLen=n[i].length();
}
char names[][]= new char[n.length][];
String s="";
for(int i=0;i<n.length;i++)
{
names[i]= n[i].toCharArray();
}
for(int i = 0; i < maxLen; i++)
{
for(int j = 0; j < names.length ; j++)
{
if(i<names[j].length)s+= names[j][i];
}
s+=" ";
}
return s;
}
public static void main(String s[])
{
String names[] = new String[6];
Scanner sc= new Scanner(System.in);
System.out.println("Enter six names");
for(int i=0;i<6;i++)
{
names[i]=grabFirstToken(sc.nextLine());
//System.out.println(names[i]);
}
System.out.println(shuffleNames(names));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.