CodeGym
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/Struggling with isGreaterThan(String a, String b) method....
Jakhongir Ruziev
Level 23
Tashkent
  • 11.04.2020
  • 1049views
  • 8comments

Struggling with isGreaterThan(String a, String b) method. Did not quite get how it works. Can someone please clarify on that. I would really appreciate it🙏

Question about the task Task about algorithms
Java Syntax,  Level 9,  Lesson 11
Resolved

Task: The user enters a list of words (and numbers) from the keyboard. The words are displayed in ascending order, the numbers in descending order.

Example input:
Cherry
1
Bob
3
Apple
22
0
Watermelon

Example output:
Apple
22
Bob
3
Cherry
1
0
Watermelon

Requirements:
  • The program must read data from the keyboard.
  • The program should display data on the screen.
  • The displayed words should be sorted in ascending order (using the provided isGreaterThan method).
  • The displayed numbers must be sorted in descending order.
  • The main method should use the sort method.
  • The sort() method should call the isGreaterThan() method.
  • The sort() method should call the isNumber() method.
package com.codegym.task.task09.task0930; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; /* Task about algorithms */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<String>(); while (true) { String s = reader.readLine(); if (s.isEmpty()) break; list.add(s); } String[] array = list.toArray(new String[list.size()]); sort(array); for (String x : array) { System.out.println(x); } } public static void sort(String[] array) { // write your code here for ( int i=0; i<array.length-1; i++ ) { if ( isNumber(array[i]) ) { for (int j=i+1; j<array.length; j++ ) if ( isNumber(array[j]) ) { int a = Integer.parseInt(array[i]); int b = Integer.parseInt(array[j]); if ( a<b ) { array[i] = array[j]; array[j] = String.valueOf(a); } } } else { for (int j=i+1; j<array.length-1; j++) { if ( !isNumber(array[j]) ) { String a = array[i]; isGreaterThan(array[j], array[i]); array[i]=array[j]; array[j]=a; } } } } } // String comparison method: 'a' is greater than 'b' public static boolean isGreaterThan(String a, String b) { return a.compareTo(b) > 0; } // Is the passed string a number? public static boolean isNumber(String s) { if (s.length() == 0) return false; char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if ((i != 0 && c == '-') // The string contains a hyphen || (!Character.isDigit(c) && c != '-') // or is not a number and doesn't start with a hyphen || (i == 0 && c == '-' && chars.length == 1)) // or is a single hyphen { return false; } } return true; } }
0
Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Ntuthuko Xaba
Level 18 , Johannesburg, South Africa
11 April 2020, 21:48
.compareTo() , is actually a String method that compares strings. eg. Just as int 5 is > int 2, so in strings the String ' D ' (4) is > ' B' (2). Hope that makes sense.
0
Jakhongir Ruziev QA Automation Engineer at Exadel
12 April 2020, 06:52
Yes thank you, that makes sense.
0
Ntuthuko Xaba
Level 18 , Johannesburg, South Africa
11 April 2020, 21:44
On line 50 - it should be: for (int j = 0; j < array.length; j++) { On line line 54 you made a mistake of getting [i] but you don’t have [i] initialised in this particular codeblock. The [i] you are calling is from the ' if ' codeblock. You need to insert the following before line 54: for (int i = 0; i < array.length; i++) { After this it should be fairly simple to figure the rest out.
0
Jakhongir Ruziev QA Automation Engineer at Exadel
12 April 2020, 06:57
i is initialized at the beginning of the for loop(line 32) if i is number it goes to first block(line 34) else goes to second(line 48). And then it checks for j if it is not number(line 52) it iterates through them. But if I enter only words it is not sorting, that is why I don't get why it is not working 🤷‍♂️
0
Ntuthuko Xaba
Level 18 , Johannesburg, South Africa
12 April 2020, 14:08solution
I'm not sure where you got it wrong but you can look at my code and compare. public static void sort(String[] array) { //Lets sort the numbers for (int i = 0; i < array.length; i++) { String a = array[i]; if(isNumber(a)){ for (int j = 0; j < array.length; j++) { String b = array[j]; if(isNumber(b)){ int aa = Integer.parseInt(array[i]); int bb = Integer.parseInt(array[j]); if( aa > bb){ String temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } //Lets sort words for (int i = 0; i < array.length; i++) { String a = array[i]; if(!isNumber(a)){ for (int j = 0; j < array.length; j++) { String b = array[j]; if(!isNumber(b)){ if(isGreaterThan(b,a)){ String temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } }
+3
Liliane Top
Level 17 , Amsterdam, Netherlands
12 April 2020, 15:56
Thanks to you I did find the solution and also found what is not working in your code.
while(isGreaterThan(array[i], array[j])){ //as long as the first String comes before the next by checking its individual chars
                        String temp = array[i];//swap position
                        array[i] = array[j];
                        array[j] = temp;
                    }
As long the following is true is keeps swapping positions. isGreaterThan(array[i], array[j]) returns true (>0) only if the String value of array[i] is greater than array[j]. Otherwise it would return a '0' if they are equal or '-1' if it is smaller. And nothing will be changed. So in case it's value is greater you swap their position.
0
Liliane Top
Level 17 , Amsterdam, Netherlands
12 April 2020, 15:58
Oh i forgot to tell you that I also changed the 'array.length -1' to 'array.length' otherwise you will not get to the last item in the array. So either one of the for loops should check the last item.
0
Jakhongir Ruziev QA Automation Engineer at Exadel
13 April 2020, 02:38
Thank you guys!
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym