How to set print outputs equal to a variable in Java? -


i'm new java , programming if question painfully obvious apologize in advance.

what i'm trying set printed output of line equal variable can use variable add or subtract .3 depending on whether or not "-" or "+" sign typed in input.

edit:

import java.util.*; public class homeworkcalculator { public static void main(string[]args) {  scanner in = new scanner(system.in); string grade, letter1, letter2; system.out.print("enter letter grade: "); grade = in.nextline(); letter1 = grade.substring(0,1); letter2 = grade.substring(1,2);  double gpa = 0;  if (letter1.equals("a")) gpa = 3.9; else if(letter1.equals("b"))  gpa = 3.0; else if(letter1.equals("c")) gpa = 2.1; else if(letter1.equals("d")) gpa = 1.2; else if(letter1.equals("f")) gpa = 0;  system.out.println(gpa);  if(letter2.equals("+")) {    system.out.println(gpa + 0.3); }   else if(letter2.equals("-")) { system.out.println(gpa - 0.3); }}} 

so problem if enter single letters a,b,c,d,f gives me error. i'd program accept single-lettered grades in addition defaulting input of 'a+' no higher 4.0, , making inputs f+ or f- return same gpa of 0 grade f. i'll fool around more if/else statements , logical operators..:

exception in thread "main" java.lang.stringindexoutofboundsexception: string index out    of range @ java.lang.string.substring(string.java:1907) @ homeworkcalculator.main(homeworkcalculator.java:10) 

i see multiple problems:

  • else if(letter.equals("-")) means test if plusminussign.equals("-")
  • you start else if instead of if
  • you hardcode print result 3.0 instead of using variable.

here's think wanted (also remember create variable x lower case):

double x = 0; // or whatever want x  if(letter.equals("b")) {     x = 3; } system.out.println(x);  if(plusminussign.equals("+")) {     // x = x + 0.3; // don't know if want modify variable     system.out.println(x + 0.3); } else if(plusminussign.equals("-")) {     // x = x - 0.3; // don't know if want modify variable     system.out.println(x - 0.3); } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -