-1

I'm very new to programming and very very new to Java, so I'm sure this question (and all my other mistakes) will be very obvious to anyone with experience. I clearly don't know what I'm doing. My assignment was to convert Python code to Java. Below is my code.

My two error codes are:

Assignment1.java:37: error: incompatible types: String cannot be
converted to int 
response = reader.nextLine();
Assignment1.java:38: error: incompatible types: int cannot be converted to String 
num = Integer.parseInt(response);

For clarification, I'm using JGrasp, but willing to not.

Thanks in advanced the help. ~JBOrunon

import java.util.Scanner;
public class Assignment1 {
      public static void main(String[] args) {

         int num, response, min, max, range, sum, count, avg;
         Scanner reader = new Scanner(System.in);
         String reportTitle;


         //title
         System.out.println();
         System.out.println("Enter a title for this report");
         reportTitle = reader.nextLine();
         System.out.println();


         //data input
         System.out.println("Enter a series of numebr, ending with a 0");

         num = 0;
         sum = 0;
         response = 1;
         count = 0;
         max = 0;
         min = 0;
         range = 0;

         while (true)
         {
            if (response < 1)
            {
               System.out.println("End of Data");
               break;
            }

          System.out.println("Enter number => ");
          response = reader.nextLine();
          num = Integer.parseInt(response);

          sum += num;
          count += 1;

          if (num>max)
          {
            max = num;
          }
          if (num<min)
          {
            min = num;
          }

         }

         //crunch
         avg = sum / count;
         range = max - min;

         //report
         System.out.println();
         System.out.println(reportTitle);
         System.out.println();
         System.out.println("Sum: => "+sum);
         System.out.println("Average: => "+avg);
         System.out.println("Smallest: => "+min);
         System.out.println("Largest: => "+max);
         System.out.println("Range: => "+range);


    }

}
Johann Boniek
  • 23
  • 1
  • 5
  • 1
    `response` is an `int` and `Scanner.nextLine()` returns a `String`. How would you convert a line containing i.e. `this line contains letters and 6 spaces` to an `int`..? – Fureeish Jan 19 '18 at 19:09
  • This is because "reader.nextLine();" returns String, and you are trying to assign it to variable of type int which is not possible. So change the data type of response to String. – AmitB10 Jan 19 '18 at 19:10
  • So @Amit1011 do you have any suggestions for what should I use in lieu of `reader.nextLine()` so it will return an int? `response` should only be a number, if all goes well – Johann Boniek Jan 19 '18 at 19:27
  • @JohannBoniek do this- `response = Integer.parseInt(reader.nextLine());`. But beware, that if someone here inputs any character instead of a number, you will get NumberFormatException. – AmitB10 Jan 19 '18 at 19:31

2 Answers2

1

response is an int and the method nextLine() of Scanner returns... well, a line represented by a String. You cannot assign a String to an int - how would you do so? A String can be anything from "some text" to " a 5 0 ". How would you convert the first and/or the second one to an int?

What you probably wanted is:

System.out.println("Enter number => ");
response = reader.nextInt();
// notice this        ^^^

instead of:

System.out.println("Enter number => ");
response = reader.nextLine();
// instead of this    ^^^^

the nextInt() method expects a sequence of digits and/or characters that can represent a number, for example: 123, -50, 0 and so on...

However, you have to be aware of one significant issue - mixing nextLine() and nextInt() can lead to undesirable results. For example:

Scanner sc = new Scanner(System.in);
String s;
int a = sc.nextInt();
s = sc.nextLine();
System.out.println(s);

Will print nothing, because when inputting the number, you type for example 123 and hit enter, you additionally input a newline character. Then, nextLine() will fire and will scan every character up to the first enountered newline or end-of-file character which apparently is still there, unconsumed by nextInt(). To fix this and read more about it go here

Fureeish
  • 9,869
  • 3
  • 23
  • 46
  • This is an immensely helpful response, thank you. Since you've been so helpful: is the reason you just mention the reason I received the other error? `Assignment1.java:38: error: incompatible types: int cannot be converted to String num = Integer.parseInt(response);` – Johann Boniek Jan 19 '18 at 19:52
  • Similar one, but the the exact same. Notice the fact that `response` is an `int` and `Integer.parseInt()` expects a **`String`** as an argument, not an `int`. Therefore, calling `Integer.parseInt(response)` results in compilation error, since you cannot pass an `int` to that method. If you apply the changes suggested in my answer, you can get rid of the line 38, since you already read an `int` and there is no need for parsing – Fureeish Jan 19 '18 at 20:00
  • @JohannBoniek consider accepting the answer if it solved your issue. It will inform the people that possibly encounter the same problem that this solution works – Fureeish Jan 20 '18 at 13:59
1

To read an integer from a file you can simply use an example like this:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

However, you dont give further information about your file structure so my advice is to look again in the documentation to find what best fits you. You can find the documentation of scanner here.

Rudziankoŭ
  • 8,411
  • 10
  • 66
  • 146