#i need help with my code

40 messages · Page 1 of 1 (latest)

turbid agate
#
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    String[] day = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

    for (int i = 0; i < day.length; i++) {
      System.out.println("How many pills do you take on " + day[i] + "?");

      int pills [] = in.nextInt();
      
      }
    for(int j = 0; j < day.length; j ++){
      System.out.print("On" + day[j] + "number of pillbox = " + pills[j]);
    }
  
    }

  }

this is my code to check how many pills was taken on certain day.

zinc quailBOT
#

This post has been reserved for your question.

Hey @turbid agate! 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.

turbid agate
#

this is my error

austere cradle
#

I think nextInt returns an int, and I guess pills is an array of ints - but you can't just assign an int to the array, I guess you need to store it at a specific position?

turbid agate
#

how do i do that

rich cape
#

the second error is because you're creating the array inside the loop, so it only exists inside that loop

#

you need to create it before, and then you can use i to choose an index in that array

turbid agate
#

first loop or my 2nd loop

#

oh

#

do i change j to i?

rich cape
rich cape
turbid agate
#

im still having issu on the first error

austere cradle
#

Can you post the code again? Also have you tried debugging it?

zinc quailBOT
#
Custom Tag List

ask, debug, example, format, js, try, resources, adopt, codelength, null-pointer, restart, homework, comparing-strings, index-out-of-bounds, comparison-assignment-operators, rule11, xyproblem, static, nohello, jre_jdk, learn, help, scanner

turbid agate
#
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    String[] day = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    

    for (int i = 0; i < day.length; i++) {
      System.out.println("How many pills do you take on " + day[i] + "?");

      int pills = in.nextInt();
      
      }
    for(int i = 0; i < day.length; i ++){
      System.out.print("On" + day[i] + "number of pillbox = " + pills[i]);
    }
  
    }

  }
turbid agate
austere cradle
#

yes

turbid agate
#

so i do int[] pills = new int[i];

#

before for loop?

austere cradle
#

yes

#

but if you know its size already (it will be one slot for each day) you can also specify its size, and then you can put data with pills[i]

#

I think if you dont set its size before, you cant just store values at indices, because the array will be empty and you would have to create these slots before putting data into them

turbid agate
#

ok my error says i is not defined

austere cradle
#

you can only use i inside the for loops

austere cradle
#

you dont need i when you define the array

turbid agate
#

but if i want to use i i would put it inside for loop?

#

it worked!!

#

tyvm

austere cradle
#

yw 🙂

turbid agate
#

This was my teachers code but it didn’t work

austere cradle
#

yes it seems there are a few parts missing

turbid agate
#

i told her that i got an error from her code, but she said it should be working.

#

whats missing in her code?

zinc quailBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

austere cradle
#

Just to start, in line 5 the list of weekdays is not complete ...

#

for line 15 there is a loop missing to print it for each day, etc