简体   繁体   English

在 Java 中,如何将自动输入的首字母大写?

[英]In Java, How do I capitalize the first letters of an automated input?

I am trying to get the auto grader/compiler for my assignment to give me points for the exact match required for the automated input.我正在尝试为我的作业获取自动评分器/编译器,以便为我提供自动输入所需的精确匹配的分数。 Here is my output: Enter playlist's title: DANCE LIST PLAYLIST MENU这是我的输出:输入播放列表的标题:DANCE LIST PLAYLIST MENU

Here is the expected output: Enter playlist's title: Dance List PLAYLIST MENU这是预期的输出:输入播放列表的标题:Dance List PLAYLIST MENU

Here are my code pages.这是我的代码页。 I do got some other issues in this code when submitted to ZyBooks also.当提交给 ZyBooks 时,我在这段代码中也遇到了一些其他问题。 I can post them later.我可以稍后发布它们。 Thank you for any help.感谢您的任何帮助。

public class SongEntry {

   private String uniqueID;

   private String songName;

   private String artistName;

   private int songLength;

   SongEntry nextNode;

   SongEntry() {

       uniqueID = "";

       songName = "";

       artistName = "";

       songLength = 0;

       nextNode = null;

   }

   SongEntry(String uniqueID, String songName, String artistName, int songLength) {

       this.uniqueID = uniqueID;

       this.songName = songName;

       this.songLength = songLength;

       this.artistName = artistName;

       this.nextNode = null;

   }

   public void insertAfter(SongEntry entry) {

       SongEntry entries = this;

       while (entries.nextNode != null) {

           entries = entries.nextNode;

       }

       entries.nextNode = entry;

   }

   public void setNext(SongEntry entry) {

       this.nextNode = entry;

   }

   public String getID() {

       return this.uniqueID;

   }

   public String getSongName() {

       return this.songName;

   }

   public String getArtistName() {

       return this.artistName;

   }

   public int getSongLength() {

       return this.songLength;

   }

   public SongEntry getNext() {

       return this.nextNode;

   }

   public void printPlaylistSongs() {

       System.out.println("Unique ID: " + getID());

       System.out.println("Song Name: " + getSongName());

       System.out.println("Artist Name: " + getArtistName());

       System.out.println("Song Length(in seconds): " + getSongLength());

   }

   public void setSongName(String songName2) {

       this.songName = songName2;

   }

   public void setUniqueID(String id) {

       this.uniqueID = id;

   }

   public void setArtistName(String artistName2) {

       this.artistName = artistName2;

   }

   public void setSongLength(int songLength2) {

       this.songLength = songLength2;

   }

}
import java.util.Scanner;

public class Playlist {

   public static Scanner sc = new Scanner(System.in);

   public static Scanner scInt = new Scanner(System.in);

   public static SongEntry headSong = new SongEntry();

   public static SongEntry tailSong = new SongEntry();

   public static SongEntry allEntries;

   public static int numberOfNodes = 0;

   public static void printMenu(String playlistTitle)

   {

   System.out.println("\n"+playlistTitle.toUpperCase()+" PLAYLIST MENU");

   System.out.println("a - Add song\nd - Remove song\nc - Change position of song\ns - Output songs by specific artist");

   System.out.println("t - Output total time of playlist (in seconds)\no - Output full playlist\nq - Quit");

   System.out.println("\nChoose an option:");

   String option = sc.next();

   boolean isEnter = option.equals("a") || option.equals("d") || option.equals("c") || option.equals("s") || option.equals("t") || option.equals("o") || option.equals("q");

   if(isEnter)

   {

   switch(option.charAt(0))

   {

   case 'a': addSong();

   printMenu(playlistTitle);

   break;

   case 'd': allEntries = removeSong(allEntries);

   printMenu(playlistTitle);

   break;

   case 'c': allEntries = changeSongPosition(allEntries);

   printMenu(playlistTitle);

   break;

   case 's': songsBySpecificArtist(allEntries);

   printMenu(playlistTitle);

   break;

   case 't': totalTimeofPlaylist(allEntries);

   printMenu(playlistTitle);

   break;

   case 'o': outputFullPlaylist(allEntries);

   printMenu(playlistTitle);

   break;

   case 'q': break;

   }

   }

   else

   {

   System.out.println("Invalid Choice !");

   printMenu(playlistTitle);

   }

   }

   public static void outputFullPlaylist(SongEntry entries)

   {

   int counter = 1;

   if(entries != null)

   {

   System.out.println(counter+".");

   entries.printPlaylistSongs(); // head node

   counter++;

   while(entries.nextNode != null) // all the remaining nodes

   {

   entries = entries.nextNode;

   System.out.println(counter+".");

   entries.printPlaylistSongs();

   counter++;

   }

   }

   else

   {

       System.out.println("Playlist is empty");

   }

   }

   public static void addSong()

   {

   sc = new Scanner(System.in);

   System.out.println("ADD SONG");

   System.out.println("Enter song's Unique ID: ");

   String songID = sc.next();

   sc = new Scanner(System.in);

   System.out.println("Enter song's name: ");

   String songname = sc.nextLine();

   sc = new Scanner(System.in);

   System.out.println("Enter artist's name: ");

   String artistName = sc.nextLine();

   System.out.println("Enter song's length(in seconds): ");

   int songlength = scInt.nextInt();

   SongEntry entry = new SongEntry(songID, songname, artistName, songlength);

   if(allEntries == null)

   {

   headSong = entry; // this is the head

   allEntries = entry;

   tailSong = entry; // this is the tail

   numberOfNodes++;

   }

   else

   {

       allEntries.insertAfter(entry);

   tailSong = entry;

   numberOfNodes++;

   }

   }

   public static SongEntry removeSong(SongEntry entries)

   {

   System.out.println("Enter the song's unique ID: ");

   String id = sc.next();

   SongEntry newEntry = null, entry=null;

   int counter = 0;

   while(entries != null)

   {

       if(counter!=0)

       {

           newEntry.nextNode = null;

           newEntry = newEntry.nextNode;

       }

       if(!entries.getID().equals(id))

       {

           newEntry = new SongEntry();

           newEntry.setUniqueID(entries.getID());

           newEntry.setSongName(entries.getSongName());

           newEntry.setArtistName(entries.getArtistName());

           newEntry.setSongLength(entries.getSongLength());

           if(entry == null)

               entry = newEntry;

           else

               entry.insertAfter(newEntry);

           counter++;

       }

       else

       {

           System.out.println(entries.getSongName()+" removed");

           numberOfNodes--;

       }

       entries = entries.nextNode;

   }

   return entry;

   }

   public static SongEntry changeSongPosition(SongEntry entries)

   {

   System.out.println("CHANGE POSITION OF SONG");

   System.out.println("ENTER song's current position: ");

   int currentPos = scInt.nextInt();

   System.out.println("Enter new position of song: ");

   int newPos = scInt.nextInt();

   SongEntry currentPosEntry = null, entry = null, newPosEntry = null, returnEntry = null;

   entry = entries;

   int counter = 1;

// System.out.println("Number of nodes: " + numberOfNodes);

   if(newPos<1)

       newPos = 1;

   else if(newPos>numberOfNodes)

       newPos = numberOfNodes;

   System.out.println("cuurent pos: "+currentPos);

   System.out.println("new pos: "+newPos);

   for(int i=1; i<=numberOfNodes; i++)

   {

      if(i==currentPos)

       currentPosEntry = entries;

      else if(i==newPos)

          newPosEntry = entries;

      else

          entries = entries.nextNode;

   }

// System.out.println("After for loop");

   //System.out.println("Current song details" ); currentPosEntry.printPlaylistSongs();

// System.out.println("New song details"); newPosEntry.printPlaylistSongs();

   entries = entry;

   while(counter <= numberOfNodes+1)

   {

       if(counter == currentPos) // we need to adjust the current position

       {

           entries = entries.nextNode;

           if(entries !=null)

           {

           entry = new SongEntry(entries.getID(), entries.getSongName(), entries.getArtistName(), entries.getSongLength());

           if(returnEntry == null)

               returnEntry = entry;

           else

               returnEntry.insertAfter(entry);

           entries = entries.nextNode;

           }

           counter++;

       }

       else if(counter == newPos)

       {

           entry = currentPosEntry;

           entry.nextNode = null;

           if(returnEntry == null)

               returnEntry = entry;

           else

               returnEntry.insertAfter(entry);

           counter++;

       }

       else

       {

           if(entries !=null)

           {

           entry = new SongEntry(entries.getID(), entries.getSongName(), entries.getArtistName(), entries.getSongLength());

           if(returnEntry == null)

               returnEntry = entry;

           else

               returnEntry.insertAfter(entry);

           entries = entries.nextNode;

           }

           counter++;

       }

   }

return returnEntry;

   }

   public static void totalTimeofPlaylist(SongEntry entries)

   {

   System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");

   int totalSeconds = entries.getSongLength();

   entries = entries.nextNode;

   while(entries != null)

   {

   totalSeconds += entries.getSongLength();

   entries = entries.nextNode;

   }

   System.out.println("Total Time: "+totalSeconds+" seconds");

   }

   public static void songsBySpecificArtist(SongEntry entries)

   {

   sc = new Scanner(System.in);

   System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");

   System.out.println("Enter artist's name: ");

   String artistname = sc.nextLine();

   while(entries != null)

   {

   if(entries.getArtistName().equals(artistname))

   {

   entries.printPlaylistSongs();

   }

   entries = entries.nextNode;

   }

   }

   /**

   * @param args

   */

   public static void main(String[] args) {

   System.out.println("Enter playlist's title:");

   sc = new Scanner(System.in);

   String title = sc.nextLine();

   printMenu(title);

   }

}

Your problem is with toUppperCase() which capitalizes the entire string.您的问题在于 toUppperCase() 将整个字符串大写。

public static String capitalizeWord(String str){  
    String words[]=str.split("\\s");  
    String capitalizeWord="";  
    for(String w:words){  
        String first=w.substring(0,1);  
        String afterfirst=w.substring(1);  
        capitalizeWord+=first.toUpperCase()+afterfirst+" ";  
    }  
    return capitalizeWord.trim();  
} 

To print you would do:要打印,您将执行以下操作:

//to ensure all other letters are lowercase
playlistTitle.toLowerCase();
//to capitalize first letters
System.out.println("\n"+ capitalizeWord(playlistTitle) +" PLAYLIST MENU");

capitalizeWord() found at https://www.javatpoint.com/java-program-to-capitalize-each-word-in-string capitalizeWord() 位于https://www.javatpoint.com/java-program-to-capitalize-each-word-in-string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Java将Textfield中的首字母大写 - Capitalize first letters in a Textfield in Java 如何将java中字符串中每个单词的第一个和最后一个字母大写 - How to capitalize the first and last letters of every word in a string in java 如何在Java 8中将三个字母的月份名称日期的第一个字母大写 - How to capitalize the first letter of the three letters month name date in Java 8 使用 java 8 stream 将字符串中单词的首字母大写 - Capitalize first letters in words in the string with different separators using java 8 stream 我如何将 Java 中单词的第一个字母大写? - How would I capitalize the first letter of a word in Java? 使用Java中的Textfield中的所有字母大写 - Capitalize all letters in a Textfield in Java 如何将Java中的String首字母大写? - How to capitalize the first letter of a String in Java? 我该如何更换 <input> 字母与其他字母onclick? - How do i replace <input> letters with other letters onclick? 使用Java,如何接受用户输入中包含数字和字母的短语并对其进行分类? - Using Java, how do I accept and categorize a phrase that has numbers and letters in user input? 在 java 如何检查输入的第一个值是否为数字? - In java How do I check that the first value of a input is a number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM