How can i put a value into each slot of the array. Meaning i have:
String name[] = new name[50];
for (int i=0; int<=name; int++;){
name[1] = "name 1";
name[2] = 'name 2";
}
This is what i have so far but i know that it is not right. I could make 50 different array and give name i could do name[1] = new name["kevin"]; and so on and keeping that for all 50 people.But that so many lines of code.
so how do i give all 50 a name. and i need to do in one loop or something like that.
or can i do this:
people[] people = new people[50];
If you want to assign a name to all of them according to the pattern name index you do it like this
for (int i = 0; i < name.length; i++) {
name[i] = "name " + (i + 1);
}
and by the way, initialize your array like this
String name[] = new String[50];
open Scanner for input, Take name as input and then loop over 50 times
// open Scanner for input
Scanner keyboard = new Scanner(System.in);
String name[] = new String[50];
for (int i=0; i< name.length; i++){
//take input
System.out.println("Input");
String input = keyboard.nextLine();
name[i] = input;
}
Related
how do I make two arraylist with the same size
like when I stop at index 5 in the first arraylist the second array list will automatically stop when i reach the index 5
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList <String> title = new ArrayList<>();
ArrayList <String> description = new ArrayList<>();
int i = 0;
int d = 0;
String n = in.nextLine();
while(!n.equals(" ")){
System.out.println("Enter a movie title");
title.add(n);
n = in.nextLine();
}
for(;i < title.size(); i++){
System.out.println("[" + i+"]" +title.get(i));
}
description = new ArrayList<>(title.size());
String m = in.nextLine();
while(!m.equals(" ")){
System.out.println("Enter the movie Description");
description.add(m);
m = in.nextLine();
}
description.remove(0);
for(;d < description.size(); d++){
System.out.println("Description for ["+d+"]"+title.get(d)+":"+description.get(d) );
}
}
}
Add a break statement.
// I'm assuming here is where you want it.
// description = new ArrayList<>(title.size());
String m = in.nextLine();
while(!m.equals(" ") && description.size() < title.size()){
System.out.println("Enter the movie Description");
description.add(m);
m = in.nextLine();
}
If you really want the same amount, then don't allow them to exit.
String m = in.nextLine();
while(description.size() < title.size()){
System.out.println("Enter the movie Description");
description.add(m);
m = in.nextLine();
}
for(;d < description.size() && d < title.size(); d++)
This still does not ensure that there are equally many titles and descriptions.
Perhaps you also want to move int i = 0; and int d = 0; into the head of their corresponding for-loops (like for(int i = 0; i < title.size(); i++)). And you may want to change your while-loops to do-while-loops to avoid inserting (and even waiting/"asking" for) the first input line, which will be read before your prompt is written.
Hey guys this is my first question so if i have any mistakes or faults , i am sorry about that.
So i am working on one thing which i'm currently keep failing of and that is ,as it says in the title, reading only strings and integers from .txt file. Here is my code:
File file = new File("C:\\Users\\Enes\\Desktop\\test.txt");
Scanner scn = new Scanner(file);
String[] s = new String[10];
int i = 0;
int[] fruits = new int[10];
while (scn.hasNextInt()) {
fruits[i++] = scn.nextInt();
}while (scn.hasNext()) {
s[i++] = scn.next();
}
for (int elm : fruits) {
System.out.print(elm + "\t");
System.out.println("\n");
}
for (String ele : s) {
System.out.print(ele + "\t");
}
And here is what writes on .txt file
Apple 5
Pear 3
Output is like:
0 0 0 0 0 0 0 0 0 0
Apple 5 Pear 3 null null null null null null
So i want to get Apple and Pear, the strings in different array and 5 and 3 which is integers in different array. How can i do this? Any help would be so appreciated. Thanks all!
First, I'd rename your variables to something useful:
String[] names = new String[10];
int[] counts = new int[10];
Right now, you're trying to grab all 10 numbers and then all 10 names. But that isn't how your data is laid out.
I would use the scanner to grab the line, and split it from there:
Scanner sc = new Scanner(new File(file));
int index = 0;
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split(" ");
names[index] = tokens[0];
counts[index] = Integer.parseInt(tokens[1]);
index++;
}
For output, we iterate both loops at the same time:
for(int i = 0; i < 10; i++) {
System.out.println(names[i] + "\t" + counts[i]);
}
This is an adaption of corsiKa's answer (which is-in-of-itself, correct +1), but which demonstrates using a second Scanner to parse the line ...
int index = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner lineScanner = new Scanner(line);
names[index] = lineScanner.next();
counts[index] = lineScanner.nextInt();
index++;
}
because people seem to forget that Scanner can parse String values
File info = new File("..\\userInfo2\\users.txt");
Scanner size = new Scanner(info);
Scanner read = new Scanner(info);
read.useDelimiter(",");
size.useDelimiter(",");
String ID[], name[], password[];
int count = 0, sub = 0;
//finds array size
while(size.hasNext())
{
count ++;
size.next(); //if nextLine(), then # of lines
}
name = ID = password = new String [count];
for (int i = 0; read.hasNext(); i++) {
name[i] = read.next();
read.next();
ID[i] = read.next();
password[i]= read.next();
read.next();
read.next();
System.out.println(name[i] + ID[i] + password[i]);
}
name[i] and ID[i] keep getting overwritten. in the end I always get password[i] three times. Why does this happen and how can it be fixed?
Name, ID and password are reference variables that all point to the same object. Hence any change done to any one of them is reflected on the others.
Firstly sorry for my english.
I want to ask you how can I make field of names into which I can put/save name from user.
This was my try, but every time I ran it and user write his name the first name in field was replaced by user name. I hope you understand me.
Scanner sc = new Scanner(System.in);
String[] name = {"John","Nico","Sarah","Pablo","Micheal"};
for (int i = 0; i < 1; i++)
{
System.out.println("Write your name: ");
String s = sc.nextLine();
name[i] = s;
}
Arrays.sort(name);
for (String s2 : name)
{
System.out.print(s2 + ", ");
}
}
}
Scanner scanner = new Scanner(System.in);
String[] names = {"John", "Nico", "Sarah", "Pablo", "Micheal", ""};
System.out.print("Write your name: ");
String userName = scanner.nextLine();
names[names.length - 1] = userName;
System.out.println(Arrays
.stream(names)
.sorted()
.collect(Collectors.joining(",")));
This should return what you want.
An Array always has a fixed size. By typing:
String[] names = {"John", "Nico", "Sarah", "Pablo", "Micheal"};
You define the size of the Array to be 5. By adding the extra "" you can "reserve" a place for the user's name.
Using:
names[names.length - 1] = userName;
You replace "" by the user input, instead of replacing the first name, like in your solution.
Hope this helps. Happy coding!
It looks like you are initializing the array name[] with some values and then overwriting the values with the user inputs. If you want to just initialize the array then you can use array initialization syntax.
String[] name = new String[5];
Also in the for loop the condition i < 1 is wrong. It should be i < name.length.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//String[] name = { "John", "Nico", "Sarah", "Pablo", "Micheal" };
String[] name = new String[5];
//for (int i = 0; i < 1; i++) {
for (int i = 0; i < name.length; i++) {
System.out.println("Write your name: ");
String s = sc.nextLine();
name[i] = s;
}
Arrays.sort(name);
for (String s2 : name) {
System.out.print(s2 + ", ");
}
}
In the code below I try to get the grade of all my subjects. I get the input via a for loop. But I'm supposed to store the grade ("C" for example) in the grade array but I get a NullPointerException. I don't understand what's wrong with my code since I thought it was good to go.
Also, ignore the names of the subjects since they are in Swedish.
public class Defan{
Scanner sc = new Scanner(System.in);
String grade[];
String gradeName[] = {"Svenska1", "Svenska2", "Svenska3", "Engelska5", "Engelska 6",
"Mattematik 1c", "Mattematik 2c", "Mattematik 3c", "Mattematik 4", "Idrott", "Samhälle", "Religion",
"Historia", "Biologi1", "Biologi2", "Fysik1", "Fysik2", "Kemi1", "Kemi2", "Franska3", "Franska4", "Körsång1", "Körsång2"
, "Gymnasiearbete"};
public void getInput(){
for(int i = 0; i <= gradeName.length; i++){
System.out.println("Enter grade for " + gradeName[i] + ": ");
grade[i] = sc.nextLine();
}
}
It is very obvious, you have to allocate grade array first!
In your constructor:
final int SIZE = 1024;
grade = new String[SIZE];
Actually i prefer to use an ArrayList instead of array in your case.
You are trying to use grade[] array before initializing it. Try
String grade[] = new String[gradeName.length] <br>
And in in your for loop for(int i = 0; i <= gradeName.length; i++)
In condition use only '<' instead of '<=' like this
for(int i = 0; i < gradeName.length; i++)
because 0 is the first index and size-1 is last index.