#Issue with a Variable

28 messages · Page 1 of 1 (latest)

long lintel
#

error: variable shipWeightPounds might not have been initialized is the error I keep getting

“`import java.util.Scanner;

public class ShippingCalculator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int shipWeightPounds;
int shipCostCents = 0;
final int FLAT_FEE_CENTS = 75;
final int CENTS_PER_POUND = 25;
shipCostCents = (shipWeightPounds * CENTS_PER_POUND) + FLAT_FEE_CENTS;

  System.out.println("Weight(lb): " + shipWeightPounds);
  System.out.println("Flat fee(cents): " + FLAT_FEE_CENTS);
  System.out.println("Cents per pound: " + CENTS_PER_POUND);
  System.out.println("Shipping cost(cents): " + shipCostCents);

}
}“`

I am completely new to java, and tips and resources is also helpful, but I am really confused

exotic hingeBOT
#

This post has been reserved for your question.

Hey @long lintel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

open marten
#

well. exactly as the error says, you didn't give shipWeightPounds a value

#

initialize = giving an initial value, making it ready to be used

long lintel
#

so what is the equivalent in java to having the user put an input in. like python is shipWeightPounds = int(input())

open marten
#

from the Scanner you have, use nextInt()

long lintel
#

do you have a site that shows how its used?

open marten
#

no, sorry. on mobile too so can't easily get one for ya. try javatpoint/geeksforgeeks/baeldung will probably have what you want

long lintel
#

nextInt(shipWeightPounds); or shipWeightPounds = nextInt() is all i could think of

open marten
#

Scanner is for reading strings/files, so the Scanner on System.in is like the input function

next/nextline are like input() itself, and Integer.parseInt is the equivalent for int()

nextInt is a shortcut that combines next with parseInt

open marten
long lintel
#

I got it!

#

thank you

open marten
#

fyi, function(output) is a pattern that's used/usable only in certain languages, like c/c++ and c#
neither java nor python have support for it, so you won't be seeing that pattern

long lintel
#

i learned c# first

#

then sql

#

then python

open marten
#

makes sense

long lintel
#

lol so I was like scraping my brain for whatever i could find

open marten
#

it's not an unreasonable assumption, it's wrong but understandable where you mightve got that idea from

long lintel
#

the ; at the end of each line is what made my brain compare it to c#

open marten
#

java has more similar syntax and OOP to c#, but some similar patterns to python

open marten
long lintel
#

the school im attending does 8 week courses and i feel like im being tossed around lol

open marten
#

you'll also be using parens and braces for statements unlike in python

open marten
long lintel