CodeGym
Promotion
CodeGym University
Learning
Course
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
hidden #10625598
Level 23
  • 18.07.2020
  • 234views
  • 0comments

why do i get a Nullpointer here?

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


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 String temp; for(int i = 0; i < array.length; i++){ for(int j = i+1; j < array.length; j++){ if(!isNumber(array[i]) && !isNumber(array[j])){ if(isGreaterThan(array[i], array[j])){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } else if(isNumber(array[i]) && isNumber(array[j])){ if(Integer.parseInt(array[i]) > Integer.parseInt(array[j])){ temp = array[i]; array[i] = array[j]; array[j] = array[i]; } } } } } // 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
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • 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 Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.