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 18 of 18

Thread: Concatenating Inputs, need help please!

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Concatenating Inputs, need help please!

    import java.io.*;
    public class Main {
    public static void main(String[] args) throws IOException {
    BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
    int num1, num2;
    String shape, operation, pass;

    System.out.print("Enter Operation: ");
    operation = input.readLine();
    System.out.print("Enter Number 1: ");
    num1 = Integer.parseInt(input.readLine());
    System.out.print("Enter Number 2: ");
    num2 = Integer.parseInt(input.readLine());
    OUTPUT
    Enter Operation: +
    Enter Number 1: 3
    Enter Number 2: 5

    * end of program *
    But our professor wants us to have an output like this,

    OUTPUT
    Input: 3+5

    He wants us to input 3 variables (int[3], String[+], int[5]) in one line. Is there a command that can return the cursor to the previous line so that after I enter "3", I can enter "+" next to it and not on the next line. Same goes with "5". Or is there other way to do this?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Concatenating Inputs, need help please!

    Can't you just print it all out in the end? I.e System.out.println("Input: " + n1+operator+n2);?
    Or request 1 input, everything in 1 go, then extract 1 character at a time from the input?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    "3+5" there was supposed to be an input but they are equivalent to 3 variables which is (num1, operation, num3)

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Concatenating Inputs, need help please!

    So what is wrong with taking one of the approaches I suggested? If it's not right, why isn't it? What is your program supposed to do?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    Let me put it this way. Let say I want to ADD 3 and 5. So the computer will ask me to enter/input my equation which is "3+5". Then, the computer will display the sum which is 8.

    INPUT: 3+5
    SUM: 8

    So I'm asking if how could I do the "3+5" by using 3 different variables which is an integer, a String, and an integer that the user will input in one line only.

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Concatenating Inputs, need help please!

    Maybe you want java.lang.Scanner - do you mean you type at the command prompt '3+5' all at once and your program reads the 3, then the +, then the 5?

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    @Sean4u
    Yes! that's what I'm saying. Can you help me with the java.lang.Scanner because we are not using it in our class.

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Concatenating Inputs, need help please!

    Yeah, Scanner is better.
    Look up: String.charAt()
    And: Integer.parseInt()

    Should be enough information there to get you going.
    Last edited by newbie; August 17th, 2011 at 10:01 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Concatenating Inputs, need help please!

    Can you help me with the java.lang.Scanner
    Maybe, I think a lot of other forumers are more familiar with it than I am. Your first step is to read the API documentation for it to work out if it's right for your problem, what methods might work and then to actually give it a spin. You'd use a Scanner object in place of that BufferedReader. You could use BufferedReader (I would) to do this job, but then you'd have to explicitly extract the parts of your expression using something complicated like java.util.regex.Pattern

    http://www.google.com/search?q=java%20Scanner
    http://download.oracle.com/javase/1,...l/Scanner.html

  10. #10
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    I'm sorry but I don't really know how to use Scanner. I've tried some but I ended up the same with using BufferedReader.

  11. #11
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Concatenating Inputs, need help please!

    Show us what you tried. A hint to get you going with Scanner without using java.util.regex.Pattern is to delimit your input. Put a space between each of your inputs like this: '3 + 5'. Then the Scanner methods which retrieve individual tokens can recognise where one token stops and another starts. If your professor wants you to be able to input expressions without whitespace, then things will get a little harder. If you can get your program working with Scanner and whitespace, you should be able to just 'upgrade' your input validation when you figure out how to do it. Do you think your professor want you to use Scanner, or does he want you to separate the terms of the expression without using any fancy classes from the API? You could ask him - it might be awful if you develop something fancy with Scanner, Pattern and Maps and then he says he wanted you to do it with character arrays.

  12. #12
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Concatenating Inputs, need help please!

    That sounds a little excessive Sean4u.
    A simple program which reads in 1 line, then uses String.charAt(index) to get the first, second and third character from the input String, which you can then parse into respective data types can be used to do this exactly.
    The second character can remain a String and then be compared with "+", "-", "*" and "/", and perform an arithmetic operation using the first and third character.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  13. #13
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Concatenating Inputs, need help please!

    Quote Originally Posted by newbie View Post
    A simple program which reads in 1 line, then uses String.charAt(index) to get the first, second and third character
    You're absolutely right ... if xdrechsler's professor has asked for a calculator program to add one-digit numbers. That's another thing xdrechsler could ask!

  14. #14
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    Got It!

    import java.io.*;
    import java.util.*;

    public class NewClass {
    public static void main(String args[]) throws IOException {
    BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
    int num1 = 0, num2 = 0;
    String operator = "", equation;

    System.out.print("Enter Equation: ");
    equation = input.readLine();

    Scanner scan = new Scanner(equation);

    while (scan.hasNext()) {
    if (scan.hasNextInt()) {
    if (num1 == 0) {
    num1 = scan.nextInt();
    System.out.println("NUM1: " + num1);
    }
    else {
    num2 = scan.nextInt();
    System.out.println("NUM2: " + num2);
    }
    }
    else {
    operator = scan.next();
    System.out.println("OPERATOR: " + operator);
    }
    }
    }
    }
    OUTPUT:
    Enter Equation: 20 + 5
    NUM1: 20
    OPERATOR: +
    NUM2: 5
    Can you help me with the whitespace so that I can enter "20+5" instead of "20 + 5".

  15. #15
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Concatenating Inputs, need help please!

    Good! Now do it newbie's way in post #12!

  16. #16
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    Do I have to change the entire program or I just add something to it with regards to newbie's post?

  17. #17
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Concatenating Inputs, need help please!

    What I do when I'm learning programming languages (I never stop) is I get Program5 working first and then copy it to Porgram5UsingCharAt and just change the little bit that makes it different. That way I build up a collection of techniques for compare-and-contrast. All you need to do is to delete the code that uses Scanner and use String.charAt() instead. The code from chars to numbers can be a bit pervy - you'll want to subtract a base char from your 0-9 or convert to String and use Integer.parseInt ... or something. Give it a spin. When you've got that working, then you can do it all again to cope with '3+ 5' (unreliable whitespace).

  18. #18
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Concatenating Inputs, need help please!

    Thanks! I'll give it a go later, I need to get some rest first, busy day for me.

Similar Threads

  1. get GMT Offset when latitude and longitude are inputs
    By jyothi1305 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 01:51 AM
  2. How to store 10 inputs in an arraylist?
    By Bray92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2011, 10:22 PM
  3. how to give inputs to my simple java program
    By pokuri in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 8th, 2011, 07:36 PM
  4. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 01:25 AM
  5. Concatenating string with html tag
    By subhvi in forum Java Theory & Questions
    Replies: 4
    Last Post: September 29th, 2009, 05:47 AM