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

Thread: [2ND largest number] Type everything in one line (without arrays).

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [2ND largest number] Type everything in one line (without arrays).

    Hey.

    I an assignment where i must be able to put in 10 different whole pair of numbers such as:

    19 4322 56 -234 543 -424 3 123 2 33

    and every single one of them on one line. And the program must be able to pick out the 2nd largest number!



    Okay so i can put 10 different whole numbers such as -43 11 etc on one line and get the right results.
    But i was wondering if anyone could help me figure out if i can put some sort of "stop" statment so that
    the program only wants everything on one line.


    public static void main(String[] args) {
     
    		int max = 0, second_max = 0, temp;
    		Scanner scanner = new Scanner(System.in);
    		int numbers1 = 10;
    		System.out.print("write in 10 whole numbers : ");
    		 for (int i = 0; i < numbers1; i++) {
    			if (i == 0) {
    				max = scanner.nextInt();
    			} else {
    				temp = scanner.nextInt();
    				if (temp > max) {
    					second_max = max;
    					max = temp;
    				} else if (temp > second_max) {
    					second_max = temp;
    				}
    			}
    		}
    		scanner.close();
    		System.out.println("The 2nd largest numbers are : " + second_max);
    	}
    }
     
    Last edited by MrLowBot; November 23rd, 2018 at 07:43 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,904
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: [2ND largest number] Type everything in one line (without arrays).

    each time that i hit enter and therefore it doesn't all stay on one line.
    Don't hit Enter until all 10 numbers have been entered on one line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [2ND largest number] Type everything in one line (without arrays).

    Quote Originally Posted by Norm View Post
    Don't hit Enter until all 10 numbers have been entered on one line.
    Yeah i just now figured that out haha.
    But i updated my question.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,904
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: [2ND largest number] Type everything in one line (without arrays).

    The Scanner class has some methods that might be useful.
    Basically it is hard to make a user enter data the way you want. You can detect what the user has done and issue messages to try to correct the user's response.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    220
    My Mood
    Cool
    Thanks
    0
    Thanked 24 Times in 24 Posts

    Default Re: [2ND largest number] Type everything in one line (without arrays).

    It sounds like you want a raw input mode so that characters are available as they are typed. This is not possible in Java and it presents it's own set of problems (looking for delimiters, parsing the number to ensure it's a number, etc). Do what Norm suggested in post #4. But how much correction are you allowed to do? What if someone types a word in amongst the numbers?

    Regards,
    Jim

  6. #6
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [2ND largest number] Type everything in one line (without arrays).

    Quote Originally Posted by jim829 View Post
    It sounds like you want a raw input mode so that characters are available as they are typed. This is not possible in Java and it presents it's own set of problems (looking for delimiters, parsing the number to ensure it's a number, etc). Do what Norm suggested in post #4. But how much correction are you allowed to do? What if someone types a word in amongst the numbers?

    Regards,
    Jim
    Well the program is only for numbers. So words shouldn't be a problem. It should just crash then.
    But is there a way to make sure of that the person cannot hit "enter" inbetween every whole set of number/numbers?
    Sure i can do a print line saying that they are not allowed to hit enter but that doesn't stop them for actually doing it haha.

    Any tips?

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    220
    My Mood
    Cool
    Thanks
    0
    Thanked 24 Times in 24 Posts

    Default Re: [2ND largest number] Type everything in one line (without arrays).

    The only way I know is to have them enter the numbers. If they don't enter enough, reprint the input (optional) and prompt for the rest. If they enter too many, use only what is required.

    Some operating system consoles will allow characters to be read as they are typed. Unix is one of them. But that opens up a whole new can of worms. You have to keep taking in digits until the user types a delimiter. But then, what about a number like the 10e3 which is 1000. So you need to correctly parse the input. If this is for an assignment I would discuss with the instructor to see what is acceptable/required.

    Regards,
    Jim

Similar Threads

  1. Arrays , command line arguments
    By Jad_Asmar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 30th, 2013, 10:25 AM
  2. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  3. [SOLVED] Four arrays 32 line characters from text
    By sketch_flygirl in forum Collections and Generics
    Replies: 7
    Last Post: March 31st, 2011, 10:33 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM