Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

printing - System.out.println in java working, but System.out.print is not displaying (or when I call my error it displays when then error is called)

Ok, so for learning purposes making some code. Wanted to have a nice line to print where the console would ask the user to present a day of the week. Now when I try to use system.out.println() the string displays properly and where it is supposed to be. However when I just use print() it doesn't display, or only after I call the catch-block of the errors.

What causes this and how can it be prevented in the future?

public static void dayOfTheWeek(){
      
      System.out.print("Select day:");
      
      try(Scanner sc = new Scanner(System.in)){
      int selectedDay = sc.nextInt();
      
      dayOfTheWeek2(selectedDay);
      }
      
      catch (Exception e){System.err.print(e);
      System.out.println("Faulty Selection...");}
  } // end dayOfTheWeek
  
  public static void dayOfTheWeek2(int day){
      ArrayList<String> days = new ArrayList<>();
      days.add("Monday");days.add("Tuesday");days.add("Wednesday");
      days.add("Thursday");days.add("Friday");days.add("Saturday");days.add("Sunday");
      
      int correctedSelection = day-1;
      
      if(correctedSelection > 7){
          System.out.println("Faulty selection, please re-try");
          dayOfTheWeek();
      } else {System.out.println("The day is " + days.get(correctedSelection));}

      
  } // End of dayOfTheWeek2
question from:https://stackoverflow.com/questions/65545501/system-out-println-in-java-working-but-system-out-print-is-not-displaying-or-w

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...