Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java help with String Input and Output and casting

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java help with String Input and Output and casting

    Hi everyone,

    I'm pretty new to programming and my teacher assigned me this very confusing lab. I have to create a program that uses the Scanner in String input and Output that will enable the user to convert
    double -> int
    int -> byte
    int -> short
    int -> hex-string

    The output should look something like this:
    Syntax: convert <value> <type>
    convert 15.5 int ~> 16
    convert 128 byte ~> -128
    <type>: int, byte, short, hex, decimal
    Syntax: max <type>
    max byte ~> 127
    <type>: int, byte, short

    Command ~> convert 15.5 int
    Result: 16
    Command ~> convert 128 byte
    Result: -128
    Command ~> max short
    Result: 32767
    Command ~> convert 158342834 hex
    Result: 9701eb2

    I would be eternally grateful if someone could help me with this program. Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,621
    Thanks
    53
    Thanked 2,416 Times in 2,369 Posts

    Default Re: Java help with String Input and Output and casting

    What have you tried?
    Do you have any specific questions?

    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help with String Input and Output and casting

    Its much more complex than a 3 line code, ill show you what i've got so far, i dont even know if its all right.

    import java.util.Scanner;

    public class lab5 {

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("What value and type would you like to convert to?");
    String input = scanner.nextInt();

    if (command.equals("convert")) {
    String command = ?;
    String value = ?;
    String target = ?

    if (target.equals("int")) {
    int number = Integer.parseInt( input );

    } else if (target.equals("byte")) {
    byte number = Byte.parseByte( input );

    } else if (target.equals("short")) {
    short number = Short.parseShort( input )

    } else if (target.equals("hex")) {
    System.out.println("Result:" + Integer.toHexString());

    System.out.println("Max:" + Integer.MAX_VALUE);

    } else { System.out.println("I don't know how to do that.");
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,621
    Thanks
    53
    Thanked 2,416 Times in 2,369 Posts

    Default Re: Java help with String Input and Output and casting

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Do you have any specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help with String Input and Output and casting

    That doesnt help me and i dont know how to do that. My only question is can you do it for me or tell me what I need to fix in order to get my result?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,621
    Thanks
    53
    Thanked 2,416 Times in 2,369 Posts

    Default Re: Java help with String Input and Output and casting

    Sorry, we don't do students work for them here. We'll help you get your program to work.

    Before writing any more code make a list of the simple steps the program must take to solve the problem. When you have the list of what it will take, start with the first step: code it, compile and test it. When it works, move to the second step in the list.

    If you get error messages you need help with, copy the full text and paste it here.

    --- Update ---

    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help with String Input and Output and casting

    No i understood that i give u the code and u fix it, or do it for me. Please this lab is due tomorrow for me and if i dont turn it in ill get an F.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,621
    Thanks
    53
    Thanked 2,416 Times in 2,369 Posts

    Default Re: Java help with String Input and Output and casting

    That is not correct. We help students get their projects working. We don't write their code for them.
    If you aren't going to do the work for this project, then this thread will be closed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: June 18th, 2014, 09:18 AM
  2. Casting error using String with generics
    By DOLZero in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 16th, 2013, 08:13 PM
  3. Casting java.lang.String to a custom class
    By Tyluur in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 29th, 2012, 08:51 PM
  4. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM
  5. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM