Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Can anybody help me convert into java?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
 
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What have you tried so far?

cin is done with Scanner in  Java
cout is done with System.out.print() in Java
str == "abc" is done using equals() in Java
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is what I have so far does it seem right? Also I am getting an error on the scanner.nextInt() and the string amPm.

}
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
"hour24" is a variable used to calculate the output, so no input needed for it.
"amPm" variable does need input.
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

In Java, String comparison must be done with the .equals method rather than the == operator, because the == operator compares addresses only, not contents

Also, it doesn't look like you are accepting an input value for "amPm" anywhere as you are in this C++ code:


In Java, the main method has no return value ("void"), unlike C++ where you return an int.
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am doing it right with the scanner.nextInt(); ?
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

E Baller wrote:This is what I have so far does it seem right? Also I am getting an error on the scanner.nextInt() and the string amPm.


You named your Scanner variable "scnr".
You didn''t assign anything to amPm.
You are not using equals() to test amPm.
 
Marshal
Posts: 4826
605
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Also, in Java we have String rather than string.
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You can use printf() to do a formatted print. This saves you the trouble of having to prepend zeros. This would replace ALL your print statements.

 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Is this much better? When i try to run this program I am getting an error.

Screen-Shot-2020-12-09-at-4.00.59-PM.png
[Thumbnail for Screen-Shot-2020-12-09-at-4.00.59-PM.png]
 
    Bookmark Topic Watch Topic
  • New Topic