1

This is the instructions.

Write a program that reads a sequence of input values and displays a bar chart of the values using asterisks. You may assume that all values are positive. First figure out the maximum value. That's value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks.

This is what I came up so far. It's all good except I need to enter a letter instead of a negative number to quit scanning. I have tried (if( < 0) things) but those didn't work.

import java.util.Scanner;

public class BarChart1 {
    public static void main(String [] args) {
        int[] arr = new int[100];
        int currentSize = 0;
        System.out.println("Enter a sequence of positive integers.  "

                + ("Enter a negative value to quit:"));
        Scanner in = new Scanner(System.in);

        while(in.hasNextInt()) {
        int num = in.nextInt();
            if (num < 0) {
                break;
            }
            else {
                arr[currentSize] = in.nextInt();
                currentSize++;
            }
        }

        //will find the max
        double max = arr[0];
        int y = 0;
        for (int i = 1; i < arr.length; i++) {
            y = i + 1;
            if(max < arr[i]) {
                max = arr[i];
                //y = i + 1;
            }

        }

        System.out.println("Max number is: " + max);
        System.out.println("Number of digits = " + y);
        System.out.println(Math.abs(-1));

        double scale = 40/max;
        System.out.println("Scale = " + scale);


        for (int i = 0; i < y; i++) {
            double h = scale * arr[i];
            if (h != 0) {
                for (int j = 1; j <= h; j ++) {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }

}

This is the result.

1
2
3
4
-1
Max number is: 4.0
Number of digits = 100
Scale = 10.0
********************
****************************************

I only need the asterisks. Everything else that is being printed is just for checking purposes.

halfer
  • 19,824
  • 17
  • 99
  • 186
CateDoge
  • 55
  • 1
  • 8
  • Your code doesn't show any of the attempts you've described. Why didn't reading the integer and checking for `< 0` work? (Do you know about the `break` keyword? - If not look it up.) What would you want to happen in case of a negative integer? Quit the application or just accepting input? – Thomas Jan 28 '19 at 10:17
  • Yes. My code doesn't describe any of the attempts I've described because the code above is at least working. < 0 din't work because can't compare with boolean. Break keyword gives me an error. For a negative integer it keeps going until I put a letter. For the negative, it accepts it but doesn't use it in the code. Like it accepts it but doesn't print anything. – CateDoge Jan 28 '19 at 10:21
  • Did you try reading the integer and checking it before either adding it to the array or breaking the loop? Can you show us, how you've attempted to do it? – Thomas Jan 28 '19 at 10:25
  • Honestly using a letter (or other non-integer) to interrupt the loop doesn't seem like a bad idea to me given (quoting your question) that "You may assume that all values are positive." – Aaron Jan 28 '19 at 10:26
  • @Aaron It's not a bad idea but instructions specifically said negative numbers make it quit not letters. – CateDoge Jan 28 '19 at 10:29
  • @Thomas I will do just that. Give me a few minutes. – CateDoge Jan 28 '19 at 10:29
  • Then I'd define a boolean to false before the read loop, use it in cunjunction with `in.hasNextInt()` in the loop's condition and set it to true inside the read loop when you read a negative boolean, either storing the read value in a variable before testing it and storing it in the array, or checking the value stored in the array – Aaron Jan 28 '19 at 10:30
  • "Break keyword gives me an error." - Please [edit] your question to show us how you are trying to use `break;` and what error you get. – Gord Thompson Jan 28 '19 at 10:32
  • Possible duplicate of [Java Scanner won't "finish" reading input](https://stackoverflow.com/questions/9816965/java-scanner-wont-finish-reading-input) – Kandy Jan 28 '19 at 10:41
  • @Aaron so it stops accepting numbers now but the printing is broken. while(in.hasNextInt()) { arr[currentSize] = in.nextInt(); currentSize++; if (in.nextInt() < 0) { break; } } How do I format within a comment? – CateDoge Jan 28 '19 at 10:43
  • @Kandy It stops reading input now if it's a negative number. Problem is printing is broken. – CateDoge Jan 28 '19 at 10:45
  • You can use backticks (`` ` ``) to format in comments, but it's often better to edit your question with the new state of your code (at least when it seems like you're closer to a solution) – Aaron Jan 28 '19 at 10:45
  • And your current error is most likely due to the fact that you insert the value before testing whether it's negative or not, so you try to display the negative value afterward. Test beforehand so you exit the loop without inserting the negative value into the array – Aaron Jan 28 '19 at 10:48
  • @Aaron How can I use break now if it's outside a loop or a switch? because everytime I use it before the while loop it gives me cannot put outside a loop or switch error. – CateDoge Jan 28 '19 at 10:49
  • @CateDoge I think OP looking to exist from loop which means he want to stop taking inputs and forwards to next code by using negative inputs. this seems similar in asked URL. – Kandy Jan 28 '19 at 10:51
  • I hadn't read your code well... Don't use `in.nextInt()` twice in the loop ! Otherwise you'll lose half the values. You should study Namrata's answer, it has none of the pitfalls or my original comment/your current solution – Aaron Jan 28 '19 at 10:53
  • @Aaron I will try Namrata's answer. – CateDoge Jan 28 '19 at 10:54
  • @Aaron Print is still broken. – CateDoge Jan 28 '19 at 10:59
  • I guess I can just tell my teacher to let me drop the class. I'm too stupid to understand it anyway. Thanks for all the help guys. It seems the problem is not the code but me. I'm just not enough for programming. I appreciate the time and effort. – CateDoge Jan 28 '19 at 11:04
  • It requires some perseverance, certainly, but it is important not to try learning it in a rush or under pressure. A positive frame of mind also really helps - do not beat yourself up if you do not understand it immediately. When faced with intractable problems, get good sleep and look at them the following day. – halfer Jan 30 '19 at 22:25

1 Answers1

4

You can try this:

    while(in.hasNextInt()) {
     int num =in.nextInt();
          if(num <0){
              break;
             }
          else{
               arr[currentSize] = num;
               currentSize++;
            }
}
Namrata Shukla
  • 157
  • 2
  • 8
  • That's exactly what I meant, but I'd hoped the OP will show us what he's tried first, so that we can help him better understand the error of his ways ;) – Thomas Jan 28 '19 at 10:37
  • I guess it stopped the scanner from accepting negative integers but print is still broken why? – CateDoge Jan 28 '19 at 10:58
  • @CateDoge Can you please send me the incorrect output.. (and dont be dicouraged.. you are close to the solution) – Namrata Shukla Jan 28 '19 at 11:34
  • @NamrataShukla I'm failing my programming class as we speak. It seems that my classmates understand it better than me. I'm ashamed of my lack of understanding. I feel out of place. T.T – CateDoge Jan 29 '19 at 04:51